Skip to content
Browse files

sqrt(-1) is NaN, complex math is opt-in (fixes #64)

1 parent 5b74146 commit c599e5a7b7a29a2109916740d2e72b4350a521cb @sorear committed Nov 4, 2011
Showing with 9 additions and 4 deletions.
  1. +6 −1 lib/Builtins.cs
  2. +3 −3 lib/CORE.setting
View
7 lib/Builtins.cs
@@ -988,6 +988,11 @@ public partial class Builtins {
// we don't need to do nominal checking stuff here because this
// is in a method, never inlined, and as such the binder had to
// already have been called.
+ public static Variable complex_new(Variable a1, Variable a2) {
+ double d1 = a1.Fetch().mo.mro_raw_Numeric.Get(a1);
+ double d2 = a2.Fetch().mo.mro_raw_Numeric.Get(a2);
+ return MakeComplex(d1,d2);
+ }
public static Variable complex_re(Variable a1) {
return MakeFloat(Kernel.UnboxAny<Complex>(a1.Fetch()).re);
}
@@ -1290,7 +1295,7 @@ public partial class Builtins {
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));
+ return MakeFloat(Math.Sqrt(val));
}
}
View
6 lib/CORE.setting
@@ -589,14 +589,14 @@ my class Integral is Real { }
my class Num is Real {
our constant pi = 3.14159_26535_89793_238e0;
our constant e = 2.71828_18284_59045_235e0;
- our constant i = sqrt(-1);
+ our constant i = Q:CgOp { (complex_new {0} {1}) };
method Num() { self }
method perl() { defined(self) ?? ~self !! self.typename }
method ACCEPTS(\$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);
+our constant i = Num::i;
my class Int is Integral {
method niecza_quantifier_max() { self }
method niecza_quantifier_min() { self }
@@ -613,7 +613,7 @@ my class Rat is Real {
method nude() { [ self.numerator, self.denominator ] }
}
my class Complex is Numeric {
- method new($re,$im) { $re + $im\i }
+ method new($re,$im) { Q:CgOp { (complex_new {$re} {$im}) } }
method perl() { defined(self) ?? ~self !! self.typename }
method ACCEPTS(\$t) { defined(self) ?? self == $t !! $t.^does(self) }
method Complex() { self }

0 comments on commit c599e5a

Please sign in to comment.
Something went wrong with that request. Please try again.