Skip to content

Commit

Permalink
roots() function and various partially related fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Moritz Lenz committed Sep 30, 2009
1 parent 5159c42 commit cec4b04
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
9 changes: 0 additions & 9 deletions src/builtins/math.pir
Expand Up @@ -15,15 +15,6 @@ src/builtins/math.pir - Perl6 math functions
## TODO: figure out what to get working, in order to uncomment the following
## .namespace [ 'Math::Basic' ]


.sub 'roots' :multi(_, 'Integer')
.param pmc x
.param int n
.local pmc result
result = x.'roots'(n)
.return (result)
.end

=item sign

our Int multi Num::sign ( Num $x )
Expand Down
8 changes: 8 additions & 0 deletions src/setting/Any-num.pm
Expand Up @@ -47,6 +47,10 @@ class Any is also {
}
}
multi method roots($n) {
$.Complex.roots($n);
}
our Int multi method round() is export {
Q:PIR {
$N0 = self
Expand Down Expand Up @@ -201,4 +205,8 @@ multi sub sqrt(Any $x) {
$x.Num.sqrt
}

multi sub roots($x, $n) {
$x.Complex.roots($n)
}

# vim: ft=perl6
6 changes: 5 additions & 1 deletion src/setting/Complex.pm
Expand Up @@ -10,6 +10,8 @@ class Complex {
($!re * $!re + $!im * $!im).sqrt
}

multi method Complex() { self }

multi method perl() {
"Complex.new($.re, $.im)";
}
Expand Down Expand Up @@ -58,8 +60,10 @@ class Complex {
$.abs, atan2($.im, $.re);
}
multi method roots($n) {
multi method roots($n is copy) {
my ($mag, $angle) = @.polar;
return NaN if $n < 1;
$n = $n.Int;
$mag **= 1/$n;
(^$n).map: { $mag.unpolar( ($angle + $_ * 2 * pi) / $n) };
}
Expand Down
9 changes: 8 additions & 1 deletion src/setting/Str.pm
@@ -1,5 +1,5 @@
class Str is also {
method encode($encoding = 'UTF-8', $nf = '') {
multi method encode($encoding = 'UTF-8', $nf = '') {
my @bytes = Q:PIR {
.local int bin_coding, i, max, byte
.local string bin_string
Expand All @@ -21,4 +21,11 @@ class Str is also {
};
return Buf.new(|@bytes);
}

multi method Complex() {
# this is a really ugly hack for now
# we should properly parse self and also
# extract an imaginary part
(+self).Complex;
}
}

0 comments on commit cec4b04

Please sign in to comment.