Skip to content

Commit

Permalink
Add bif_abs, bif_complex_re, and bif_complex_im, typing them to prefi…
Browse files Browse the repository at this point in the history
…x:<abs>, Complex.re, and Complex.im respectively. Error conditions for big_complex_re and bif_complex_im applied to non-complex objects are wrong, but I'm not sure if that can ever come into play in practice.

Also added abs_test.pl with quick tests of these methods.
  • Loading branch information
colomon authored and sorear committed Jun 1, 2011
1 parent 864810b commit f9e93e4
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
31 changes: 31 additions & 0 deletions abs_test.pl
@@ -0,0 +1,31 @@
# vim: ft=perl6

use Test;

plan 17;

is 10.abs, 10, "10.abs == 10";
is (-10).abs, 10, "(-10).abs == 10";
is (-10).abs.WHAT, 10.WHAT, "(-10).abs and 10 have same WHAT";

{
my $big = 2 ** 200 - 42;
is $big.abs, $big, '$big.abs == $big';
is (-$big).abs, $big, '(-$big).abs == $big';
is (-$big).abs.WHAT, $big.WHAT, '(-$bi).abs and $big have same WHAT';
}

ok (10/2).abs == 10/2, "(10/2).abs == 10/2";
ok (-10/2).abs == 10/2, "(-10/2).abs == 10/2";
is (-10/2).abs.WHAT, (10/2).WHAT, "(-10/2).abs and 10/2 have same WHAT";

ok (10.Num).abs == 10.Num, "(10.Num).abs == 10.Num";
ok (-10.Num).abs == 10.Num, "(-10.Num).abs == 10.Num";
is (-10.Num).abs.WHAT, (10.Num).WHAT, "(-10.Num).abs and 10.Num have same WHAT";

ok (10 + 5i).abs == 125.sqrt, "(10 + 5i).abs == 125.sqrt";
ok (-10 - 5i).abs == 125.sqrt, "(-10 - 5i).abs == 125.sqrt";
is (-10 - 5i).abs.WHAT, "Num()", "(-10 - 5i).abs is a Num";

ok (10 + 5i).re == 10, "(10 + 5i).re == 10";
ok (10 + 5i).im == 5, "(10 + 5i).im == 5";
52 changes: 52 additions & 0 deletions lib/Builtins.cs
Expand Up @@ -525,6 +525,58 @@ class SubstrLValue: Variable {
return MakeInt(-(long)PromoteToFixInt(r1, n1));
}

public static Variable bif_abs(Variable a1) {
int r1;
P6any n1 = GetNumber(a1, NominalCheck("$x", Kernel.AnyMO, a1), out r1);

if (r1 == NR_COMPLEX) {
Complex v1 = PromoteToComplex(r1, n1);
return MakeFloat(Math.Sqrt(v1.re * v1.re + v1.im * v1.im));
}
if (r1 == NR_FLOAT) {
double v1 = PromoteToFloat(r1, n1);
return MakeFloat(v1 < 0 ? -v1 : v1);
}
if (r1 == NR_FATRAT) {
FatRat v1 = PromoteToFatRat(r1, n1);
return v1.num < 0 ? MakeFatRat(-v1.num, v1.den) : MakeFatRat(v1.num, v1.den);
}
if (r1 == NR_FIXRAT) {
Rat v1 = PromoteToFixRat(r1, n1);
return v1.num < 0 ? MakeFixRat(-v1.num, v1.den) : MakeFixRat(v1.num, v1.den);
}
if (r1 == NR_BIGINT) {
BigInteger v1 = PromoteToBigInt(r1, n1);
return MakeInt(v1 < 0 ? -v1 : v1);
}
{
long v1 = PromoteToFixInt(r1, n1);
return MakeInt(v1 < 0 ? -v1 : v1);
}
}

public static Variable bif_complex_re(Variable a1) {
int r1;
P6any n1 = GetNumber(a1, NominalCheck("$x", Kernel.AnyMO, a1), out r1);

if (r1 == NR_COMPLEX) {
Complex v1 = PromoteToComplex(r1, n1);
return MakeFloat(v1.re);
}
return MakeInt(-111);
}

public static Variable bif_complex_im(Variable a1) {
int r1;
P6any n1 = GetNumber(a1, NominalCheck("$x", Kernel.AnyMO, a1), out r1);

if (r1 == NR_COMPLEX) {
Complex v1 = PromoteToComplex(r1, n1);
return MakeFloat(v1.im);
}
return MakeInt(-111);
}

const int O_LT = 1; const int O_LE = 2; const int O_NE = 4;
const int O_EQ = 8; const int O_GE = 16; const int O_GT = 32;
const int O_IS_GREATER = O_NE | O_GE | O_GT;
Expand Down
4 changes: 3 additions & 1 deletion lib/CORE.setting
Expand Up @@ -250,6 +250,8 @@ my class Rat is Real {
my class Complex is Numeric {
method perl() { defined(self) ?? ~self !! self.typename }
method ACCEPTS(Mu $t) { defined(self) ?? self == $t !! $t.^does(self) }
method re() { Q:CgOp { (_cgop bif_complex_re {self}) } }
method im() { Q:CgOp { (_cgop bif_complex_im {self}) } }
}
my class FatRat is Real {
method perl() { defined(self) ?? ~self !! self.typename }
Expand Down Expand Up @@ -1318,7 +1320,7 @@ sub prefix:<|> (\$item) { $item.Capture }
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 prefix:<abs> ($x) { Q:CgOp { (_cgop bif_abs {$x}) } }
sub sqrt($x) { Q:CgOp { (_cgop bif_sqrt {$x}) } }
# XXX 'Order' type
sub infix:« <=> » ($a, $b) { $a < $b ?? -1 !! $a > $b ?? 1 !! 0 }
Expand Down

0 comments on commit f9e93e4

Please sign in to comment.