Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Made Real a role, fixed the trouble with log & exp multi's that gave,…
… fixed the num cmp's to return Order enum stuff, added missing round multi sub to Num, added missing methods to Real, added missing subs, enabled real-bridge.t which now passes.
  • Loading branch information
kboga committed May 8, 2012
1 parent 5ccbf3a commit 4157918
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 35 deletions.
13 changes: 4 additions & 9 deletions src/Perl6/Metamodel/BOOTSTRAP.pm
Expand Up @@ -53,7 +53,6 @@ my stub Method metaclass Perl6::Metamodel::ClassHOW { ... };
my stub Submethod metaclass Perl6::Metamodel::ClassHOW { ... };
my stub Regex metaclass Perl6::Metamodel::ClassHOW { ... };
my stub Str metaclass Perl6::Metamodel::ClassHOW { ... };
my stub Real metaclass Perl6::Metamodel::ClassHOW { ... };
my knowhow bigint is repr('P6bigint') { }
my stub Int metaclass Perl6::Metamodel::ClassHOW { ... };
my stub Num metaclass Perl6::Metamodel::ClassHOW { ... };
Expand Down Expand Up @@ -639,23 +638,20 @@ BEGIN {
Str.HOW.set_boolification_mode(Str, 4);
Str.HOW.publish_boolification_spec(Str);

# class Real is Numeric { ... }
Real.HOW.add_parent(Real, Cool);

# class Int is (Cool does) Real {
# class Int is Cool {
# has int $!value is box_target;
# ...
# }
Int.HOW.add_parent(Int, Real);
Int.HOW.add_parent(Int, Cool);
Int.HOW.add_attribute(Int, BOOTSTRAPATTR.new(:name<$!value>, :type(bigint), :box_target(1), :package(Int)));
Int.HOW.set_boolification_mode(Int, 6);
Int.HOW.publish_boolification_spec(Int);

# class Num is (Cool does) Real {
# class Num is Cool {
# has num $!value is box_target;
# ...
# }
Num.HOW.add_parent(Num, Real);
Num.HOW.add_parent(Num, Cool);
Num.HOW.add_attribute(Num, BOOTSTRAPATTR.new(:name<$!value>, :type(num), :box_target(1), :package(Num)));
Num.HOW.set_boolification_mode(Num, 2);
Num.HOW.publish_boolification_spec(Num);
Expand Down Expand Up @@ -859,7 +855,6 @@ BEGIN {
EXPORT::DEFAULT.WHO<Submethod> := Submethod;
EXPORT::DEFAULT.WHO<Regex> := Regex;
EXPORT::DEFAULT.WHO<Str> := Str;
EXPORT::DEFAULT.WHO<Real> := Real;
EXPORT::DEFAULT.WHO<Int> := Int;
EXPORT::DEFAULT.WHO<Num> := Num;
EXPORT::DEFAULT.WHO<Parcel> := Parcel;
Expand Down
8 changes: 4 additions & 4 deletions src/core/Cool.pm
Expand Up @@ -37,12 +37,12 @@ my class Cool {
method cis() { self.Numeric.cis }

proto method log(|$) {*}
multi method log() { self.Numeric.log }
multi method log($base) { self.Numeric.log($base.Numeric) }
multi method log(Cool:D: ) { self.Numeric.log }
multi method log(Cool:D: $base) { self.Numeric.log($base.Numeric) }

proto method exp(|$) {*}
multi method exp() { self.Numeric.exp }
multi method exp($base) { self.Numeric.exp($base.Numeric) }
multi method exp(Cool:D: ) { self.Numeric.exp }
multi method exp(Cool:D: $base) { self.Numeric.exp($base.Numeric) }


method roots(Cool $n) { self.Numeric.roots($n) }
Expand Down
2 changes: 1 addition & 1 deletion src/core/Duration.pm
@@ -1,4 +1,4 @@
my class Duration is Real {
my class Duration is Cool does Real {
has Rat $.x = 0;
# A linear count of seconds.

Expand Down
2 changes: 1 addition & 1 deletion src/core/Instant.pm
@@ -1,6 +1,6 @@
my class Duration {... }

my class Instant is Real {
my class Instant is Cool does Real {
has Rat $.x;
# A linear count of seconds since 1970-01-01T00:00:00Z, plus
# tai-utc::initial-offset. Thus, $.x matches TAI from 1970
Expand Down
2 changes: 1 addition & 1 deletion src/core/Int.pm
@@ -1,6 +1,6 @@
my class Rat { ... }

my class Int {
my class Int does Real {
multi method WHICH(Int:D:) {
nqp::box_s(
nqp::concat_s(
Expand Down
9 changes: 4 additions & 5 deletions src/core/Num.pm
@@ -1,5 +1,4 @@

my class Num {
my class Num does Real {
multi method WHICH(Num:D:) {
nqp::box_s(
nqp::concat_s(
Expand Down Expand Up @@ -301,17 +300,17 @@ multi infix:<**>(num $a, num $b) {


multi infix:<cmp>(Num:D \$a, Num:D \$b) {
nqp::p6box_i(nqp::cmp_n(nqp::unbox_n($a), nqp::unbox_n($b)))
Order.(nqp::p6box_i(nqp::cmp_n(nqp::unbox_n($a), nqp::unbox_n($b))))
}
multi infix:<cmp>(num $a, num $b) {
nqp::cmp_n($a, $b)
Order.(nqp::p6box_i(nqp::cmp_n($a, $b)))
}

multi infix:«<=>»(Num:D \$a, Num:D \$b) {
Order.(nqp::p6box_i(nqp::cmp_n(nqp::unbox_n($a), nqp::unbox_n($b))))
}
multi infix:«<=>»(num $a, num $b) {
Order.(nqp::cmp_n($a, $b))
Order.(nqp::p6box_i(nqp::cmp_n($a, $b)))
}

multi infix:<===>(Num:D \$a, Num:D \$b) {
Expand Down
8 changes: 5 additions & 3 deletions src/core/Numeric.pm
Expand Up @@ -6,16 +6,17 @@ my role Numeric {
}

proto method log(|$) {*}
multi method log(Cool $base) { self.log / $base.Numeric.log }
multi method log(Numeric $base) { self.log / $base.log }
multi method log(Numeric:D: Cool $base) { self.log / $base.Numeric.log }
multi method log(Numeric:D: Numeric $base) { self.log / $base.log }

method log10() { self.log / 10e0.log }

proto method exp(|$) {*}
multi method exp($base) {
multi method exp(Numeric:D: $base) {
$base ** self;
}
method roots(Cool $n) { self.Complex.roots($n.Int) }

multi method Bool(Numeric:D:) { self != 0 }

multi method gist(Numeric:D:) { self.Str }
Expand Down Expand Up @@ -169,6 +170,7 @@ multi sub ceiling(Numeric $a) { $a.ceiling }
proto sub round(|$) { * }
multi sub round($a) { $a.Numeric.round }
multi sub round(Numeric $a) { $a.round }
multi sub round(Numeric $a, $scale) { $a.round($scale) }

proto infix:<+>($a?, $b?) { * }
multi infix:<+>($x = 0) { $x.Numeric }
Expand Down
7 changes: 3 additions & 4 deletions src/core/Rat.pm
@@ -1,6 +1,6 @@
my Int $UINT64_UPPER = nqp::pow_I(2, 64, Num, Int);

my role Rational is Real {
my role Rational does Real {
has Int $.numerator;
has Int $.denominator;

Expand Down Expand Up @@ -55,15 +55,14 @@ my role Rational is Real {
}
}

# XXX: should also be Cool
my class Rat does Rational {
my class Rat is Cool does Rational {
method Rat (Rat:D: Real $?) { self }
method FatRat(Rat:D: Real $?) { FatRat.new($.numerator, $.denominator); }
multi method perl(Rat:D:) {
$.numerator ~ '/' ~ $.denominator
}
}
my class FatRat does Rational {
my class FatRat is Cool does Rational {
method FatRat(FatRat:D: Real $?) { self }
method Rat (FatRat:D: Real $?) {
$.denominator < $UINT64_UPPER
Expand Down
24 changes: 18 additions & 6 deletions src/core/Real.pm
@@ -1,14 +1,14 @@
my class Complex { ... }

# XxX role Real does Numeric { ... }
my class Real does Numeric {
my role Real does Numeric {
method Rat(Real:D: Real $epsilon = 1.0e-6) { self.Bridge.Rat($epsilon) }
method abs() { self < 0 ?? -self !! self }
proto method sign(|$) {*}
multi method sign(Real:U:) { Mu }
multi method sign(Real:D:) { self < 0 ?? -1 !! self == 0 ?? 0 !! 1 }
method conj(Real:D:) { self }
method sqrt() { self.Bridge.sqrt }
method rand() { self.Bridge.rand }
method sin() { self.Bridge.sin }
method asin() { self.Bridge.asin }
method cos() { self.Bridge.cos }
Expand Down Expand Up @@ -47,9 +47,11 @@ my class Real does Numeric {
Complex.new(self.cos, self.sin);
}
method Complex() { Complex.new(self.Num, 0e0) }
multi method log() { self.Bridge.log }
multi method log(Real $base) { self.Bridge.log($base.Bridge) }
multi method exp() { self.Bridge.exp }
proto method log(|$) {*}
multi method log(Real:D: ) { self.Bridge.log }
multi method log(Real:D: Real $base) { self.Bridge.log($base.Bridge) }
proto method exp(|$) {*}
multi method exp(Real:D: ) { self.Bridge.exp }
method truncate(Real:D:) {
self == 0 ?? 0 !! self < 0 ?? self.ceiling !! self.floor
}
Expand All @@ -76,8 +78,14 @@ my class Real does Numeric {
$int_part == 0 && self < 0 ?? '-' ~ $r !! $r;
}

method succ() { self.Bridge.succ }
method pred() { self.Bridge.pred }

method Real(Real:D:) { self }
method Bridge(Real:D:) { self.Num }
method Int(Real:D:) { self.Bridge.Int }
method Num(Real:D:) { self.Bridge.Num }
multi method Str(Real:D:) { self.Bridge.Str }
}

proto sub cis(|$) {*}
Expand Down Expand Up @@ -107,9 +115,11 @@ multi infix:«>»(Real \$a, Real \$b) { $a.Bridge > $b.Bridge }

multi infix>=»(Real \$a, Real \$b) { $a.Bridge >= $b.Bridge }

multi prefix:<->(Real \$a) { -$a.Bridge }

proto sub infix:<mod>(|$) {*}
multi sub infix:<mod>(Real $a, Real $b) {
$a - ($a div $b) * $b;
$a - ($a.Bridge.Int div $b.Bridge.Int) * $b;
}

multi prefix:<abs>(Real \$a) {
Expand All @@ -128,3 +138,5 @@ multi sub atan2(Real \$a, Real \$b = 1e0) { $a.Bridge.atan2($b.Bridge) }
# of ambiguous dispatches. So just go with (Any, Any) for now.
multi sub atan2( \$a, \$b = 1e0) { $a.Numeric.atan2($b.Numeric) }

proto sub unpolar(|$) {*}
multi sub unpolar(Real $mag, Real $angle) { $mag.unpolar($angle) }
2 changes: 1 addition & 1 deletion t/spectest.data
Expand Up @@ -596,7 +596,7 @@ S32-num/polar.t
S32-num/power.t
S32-num/rand.t
S32-num/rat.t
# S32-num/real-bridge.t # err: Fixed2 cannot compose Real because it is not composable
S32-num/real-bridge.t
S32-num/roots.t
S32-num/rounders.t
S32-num/sign.t
Expand Down

0 comments on commit 4157918

Please sign in to comment.