# New Ticket Created by fREW Schmidt # Please include the string: [perl #63874] # in the subject line of all future correspondence about this issue. # <URL: http://rt.perl.org/rt3/Ticket/Display.html?id=63874 >
mmmm, perl6 -- fREW Schmidt http://blog.afoolishmanifesto.com
diff --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