Skip to content

Commit

Permalink
Add Perl 6 versions of cis() and rand() to setting (RT #64108).
Browse files Browse the repository at this point in the history
Patch courtesy Cory Spencer <cspencer@sprocket.org>.
  • Loading branch information
pmichaud committed Mar 26, 2009
1 parent a0c6e3d commit 3bbb1c4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 45 deletions.
37 changes: 1 addition & 36 deletions src/builtins/any-num.pir
Expand Up @@ -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'('abs,int,log,chr,polar,sqrt,truncate,unpolar', 'from'=>$P0)

## pre-seed a random number generator
$P0 = new 'Random'
Expand Down Expand Up @@ -52,15 +52,6 @@ the size of that file down and to emphasize their generic,
.return ($S0)
.end

=item cis($angle)

=cut

.namespace ['Any']
.sub 'cis' :method :multi(_)
.tailcall 'unpolar'(1.0, self)
.end


.sub 'int' :method :multi(_)
.tailcall self.'truncate'()
Expand Down Expand Up @@ -94,32 +85,6 @@ error.
.end


=item rand()

=cut

.namespace []
.sub 'rand'
.param pmc x :slurpy
## 0-argument test, RT#56366
unless x goto no_args
die "too many arguments passed - 0 params expected"
no_args:
$P0 = get_hll_global ['Any'], '$!random'
$N0 = $P0
.return ($N0)
.end

.namespace ['Any']
.sub 'rand' :method
$N0 = self
$P0 = get_hll_global ['Any'], '$!random'
$N1 = $P0
$N0 *= $N1
.return ($N0)
.end


=item sqrt()

=cut
Expand Down
34 changes: 25 additions & 9 deletions src/setting/Any-num.pm
@@ -1,29 +1,45 @@
class Any is also {
our Int multi method ceiling (Num $x:) is export {
our Int multi method ceiling is export {
return Q:PIR {
$P0 = find_lex "$x"
$N0 = $P0
$N0 = self
$I0 = ceil $N0
%r = box $I0
}
}
our Int multi method floor (Num $x:) is export {
our Complex multi method cis is export {
unpolar(1.0, self)
}
our Int multi method floor is export {
return Q:PIR {
$P0 = find_lex "$x"
$N0 = $P0
$N0 = self
$I0 = floor $N0
%r = box $I0
}
}

our Int multi method round (Num $x:) is export {
our Num method rand {
return Q:PIR {
$N0 = self
$P0 = get_hll_global ['Any'], '$!random'
$N1 = $P0
$N0 *= $N1
%r = box $N0
}
}
our Int multi method round is export {
return Q:PIR {
$P0 = find_lex "$x"
$N0 = $P0
$N0 = self
$N0 = $N0 + 0.5
$I0 = floor $N0
%r = box $I0
}
}
}

our Int sub rand (*@args) {
die "too many arguments passed - 0 params expected" if @args;
1.rand
}

0 comments on commit 3bbb1c4

Please sign in to comment.