Permalink
Browse files

Add Cool.abs, sqrt, postfix:<i>, Hash.Numeric

  • Loading branch information...
1 parent 6c52f02 commit 0163f978ab7b2e5ec1333166687c7daeb909ecc1 @sorear committed May 28, 2011
Showing with 28 additions and 1 deletion.
  1. +16 −0 lib/Builtins.cs
  2. +12 −1 lib/CORE.setting
View
@@ -744,6 +744,22 @@ class SubstrLValue: Variable {
}
}
+ public static Variable bif_sqrt(Variable a1) {
+ int r1;
+ P6any n1 = GetNumber(a1, NominalCheck("$x", Kernel.AnyMO, a1), out r1);
+
+ if (r1 == NR_COMPLEX) {
+ Complex v1 = PromoteToComplex(r1, n1);
+ double angle = Math.Atan2(v1.im, v1.re) / 2;
+ if (angle < 0) angle += Math.PI;
+ double mag = Math.Sqrt(Math.Sqrt(v1.im*v1.im + v1.re*v1.re));
+ return MakeComplex(mag * Math.Cos(angle), mag * Math.Sin(angle));
+ } else {
+ double val = PromoteToFloat(r1, n1);
+ return (val > 0) ? MakeFloat(Math.Sqrt(val)) : MakeComplex(0, Math.Sqrt(-val));
+ }
+ }
+
public static Variable bif_numand(Variable v1, Variable v2) {
P6any o1 = NominalCheck("$x", Kernel.AnyMO, v1);
P6any o2 = NominalCheck("$y", Kernel.AnyMO, v2);
View
@@ -67,6 +67,8 @@ my class Cool {
method Rat($eps = 1e-6) { Q:CgOp { (bif_rat_approx {self} {$eps}) } }
method Int() { Q:CgOp { (bif_coerce_to_int {self}) } }
method Num() { Q:CgOp { (bif_coerce_to_num {self}) } }
+ method abs() { abs self }
+ method sqrt() { sqrt self }
method grep(Mu $sm) { grep $sm, @(self) }
method map($func) { map $func, @(self) }
@@ -232,10 +234,16 @@ my class Capture {
# }}}
# Scalar types {{{
my class Num is Cool {
+ our constant pi = 3.14159_26535_89793_238e0;
+ our constant e = 2.71828_18284_59045_235e0;
+ our constant i = sqrt(-1);
method Num() { self }
method perl() { defined(self) ?? ~self !! self.typename }
method ACCEPTS(Mu $t) { defined(self) ?? self == $t !! $t.^does(self) }
}
+our constant pi = 3.14159_26535_89793_238e0;
+our constant e = 2.71828_18284_59045_235e0;
+our constant i = sqrt(-1);
my class Int is Cool {
method Int() { self }
method perl() { defined(self) ?? ~self !! self.typename }
@@ -804,6 +812,8 @@ my class Hash {
method list() { Q:CgOp { (bif_hash_pairs {self}) } }
method kv() { Q:CgOp { (bif_hash_kv {self}) } }
+ method Numeric() { +@(self) }
+
method invert() {
my %new;
for self.keys -> $k { %new{self{$k}} = $k }
@@ -1296,6 +1306,7 @@ sub prefix:<^> ($limit) { 0 ..^ $limit }
sub prefix:<so> ($item) { ?$item }
sub infix:<xx> (\$list, $ct) { map { $list }, ^$ct }
sub prefix:<abs> ($x) { $x > 0 ?? $x !! -$x }
+sub sqrt($x) { Q:CgOp { (_cgop bif_sqrt {$x}) } }
# XXX 'Order' type
sub infix:« <=> » ($a, $b) { $a < $b ?? -1 !! $a > $b ?? 1 !! 0 }
# XXX polymorphic equality
@@ -1315,7 +1326,7 @@ sub infix:«~<» ($x, $y) { die "Buffer bitops NYI"; } #OK
sub infix:«~>» ($x, $y) { die "Buffer bitops NYI"; } #OK
sub prefix:<~^> ($x) { die "Buffer bitops NYI"; } #OK
sub prefix:<||> (\|$args) { die "Slicels NYI" } #OK
-sub postfix:<i> ($item) { die "Complex NYI" } #OK
+sub postfix:<i> ($item) { $item * i }
sub infix:<&> (\|$args) { die "Junctions NYI" } #OK
sub infix:<|> (\|$args) { die "Junctions NYI" } #OK
sub infix:<^> (\|$args) { die "Junctions NYI" } #OK

0 comments on commit 0163f97

Please sign in to comment.