New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
floor-ceiling-round-sign-and-abs-in-perl6 #778
Comments
From frioux@gmail.commmmm, perl6 |
From frioux@gmail.com0001-floor-ceiling-round-sign-and-abs-in-perl6.patchdiff --git a/src/builtins/any-num.pir b/src/builtins/any-num.pir
index 6dfcee6..7bf0bbe 100644
--- a/src/builtins/any-num.pir
+++ b/src/builtins/any-num.pir
@@ -21,7 +21,7 @@ the size of that file down and to emphasize their generic,
.namespace []
.sub 'onload' :anon :init :load
$P0 = get_hll_namespace ['Any']
- '!EXPORT'('abs,cis,int,log,chr,polar,sqrt,truncate,unpolar', 'from'=>$P0)
+ '!EXPORT'('cis,int,log,chr,polar,sqrt,truncate,unpolar', 'from'=>$P0)
## pre-seed a random number generator
$P0 = new 'Random'
@@ -30,17 +30,6 @@ the size of that file down and to emphasize their generic,
.end
-=item abs()
-
-=cut
-
-.namespace ['Any']
-.sub 'abs' :method :multi(_)
- $N0 = self
- $N1 = abs $N0
- .return ($N1)
-.end
-
=item chr()
=cut
diff --git a/src/classes/Int.pir b/src/classes/Int.pir
index 96b9527..eb30b61 100644
--- a/src/classes/Int.pir
+++ b/src/classes/Int.pir
@@ -20,9 +20,6 @@ Int - Perl 6 integers
intproto = p6meta.'new_class'('Int', 'parent'=>'Integer Any')
p6meta.'register'('Integer', 'parent'=>intproto, 'protoobject'=>intproto)
p6meta.'register'('BigInt', 'parent'=>intproto, 'protoobject'=>intproto)
-
- $P0 = get_hll_namespace ['Int']
- '!EXPORT'('abs', 'from'=>$P0)
.end
@@ -37,12 +34,6 @@ This is a value type, so just returns itself.
.end
-.sub 'abs' :method :multi('Integer')
- $P0 = abs self
- .return ($P0)
-.end
-
-
=item ACCEPTS()
=cut
diff --git a/src/setting/Any-num.pm b/src/setting/Any-num.pm
new file mode 100644
index 0000000..a73529a
--- /dev/null
+++ b/src/setting/Any-num.pm
@@ -0,0 +1,94 @@
+class Any is also {
+
+=begin item sign
+
+ our Int multi method sign ( Num $x: ) is export
+
+Returns 1 when C<$x> is greater than 0, -1 when it is less than 0, 0 when it
+is equal to 0, or undefined when the value passed is undefined.
+
+=end item
+
+ our Int multi method sign ( Num $x: ) is export {
+ given self {
+ when 0 { return 0 }
+ when $_ < 0 { return -1 }
+ when $_ > 0 { return 1 }
+ default { fail 'impossible default' }
+ }
+ }
+
+=begin item floor
+
+ our Int multi method floor ( Num $x: ) is export
+
+Returns the highest integer not greater than C<$x>.
+
+=end item
+
+ our Int multi method floor ( Num $x: ) is export {
+ my $truncated = self.truncate;
+ if self == $truncated {
+ return self;
+ } else {
+ given self.sign {
+ when -1 { return $truncated - 1 }
+ when 1 { return $truncated }
+ default { fail 'impossible default' }
+ }
+ }
+ }
+
+=begin item ceiling
+
+ our Int multi method ceiling ( Num $x: ) is export
+
+Returns the lowest integer not less than C<$x>.
+
+=end item
+
+ our Int multi method ceiling ( Num $x: ) is export {
+ my $truncated = self.truncate;
+ if self == $truncated {
+ return self;
+ } else {
+ given self.sign {
+ when -1 { return $truncated }
+ when 1 { return $truncated + 1 }
+ default { fail 'impossible default' }
+ }
+ }
+ }
+
+=begin item round
+
+ our Int multi method round ( Num $x: ) is export
+
+Returns the nearest integer to C<$x>. The algorithm is C<floor($x + 0.5)>.
+(Other rounding algorithms will be given extended names beginning with "round".)
+
+=end item
+
+ our Int multi method round ( Num $x: ) is export {
+ return floor($x + 0.5);
+ }
+
+=begin item abs
+
+ our Num multi method abs ( Num $x: ) is export
+
+Absolute Value.
+
+=end item
+
+ our Num multi method abs ( Num $x: ) is export {
+ given self.sign {
+ when 1|0 { return self }
+ when -1 { return -self }
+ default { fail 'impossible default' }
+ }
+ }
+
+}
+
+# vim: ft=perl6
|
From @japhbOn Sat, 2009-03-14 at 14:07 -0700, fREW Schmidt wrote:
I'm all in favor of converting things that are complex in PIR to things Lesson from the Forth world: In cases where the semantic of a high-level -'f |
The RT System itself - Status changed from 'new' to 'open' |
From frioux@gmail.com
Yeah, the main reason I did it was because it was on the rakudo wiki for -- |
From @pmichaudOn Sun, Mar 15, 2009 at 01:25:28AM -0500, fREW Schmidt wrote:
By putting floor/ceiling/round/sign/abs as a candidates for the setting Pm |
From @japhbOn Mon, 2009-03-16 at 21:08 -0500, Patrick R. Michaud wrote:
Gotcha. Sounds fine by me (as long as the Perl 6 signatures don't carry -'f |
From @pmichaudOn Mon, Mar 16, 2009 at 09:41:37PM -0700, Geoffrey Broadwell wrote:
They do carry more overhead (perhaps even a significant amount), but it's Pm |
From @japhbOn Tue, 2009-03-24 at 11:56 -0500, Patrick R. Michaud wrote:
I get the feeling that dispatch performance is going to be utterly Of course, Perl 6 allows us to optimize *different* things for speed -- /me goes back to blind faith in the coming happy place .... -'f |
From @jnthnGeoffrey Broadwell wrote:
While performance now isn't that much of a priority for me right now, Dispatch performance will indeed matter heavily. In fact, one of the few However, anything we win there we lose overall because signature binding Further, because we have a much richer type system now, there's going to Anyway, the take away from this is: yes, I do expect we'll be able to Jonathan |
From @pmichaudOther PIR-based versions of these functions were added as part of RT Resolving ticket, thanks! Pm |
@pmichaud - Status changed from 'open' to 'resolved' |
Migrated from rt.perl.org#63874 (status was 'resolved')
Searchable as RT63874$
The text was updated successfully, but these errors were encountered: