Permalink
Browse files

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...
1 parent 5ccbf3a commit 415791868b6a0f535d855bbfa66bfb67fb68fecb @kboga kboga committed May 8, 2012
Showing with 42 additions and 35 deletions.
  1. +4 −9 src/Perl6/Metamodel/BOOTSTRAP.pm
  2. +4 −4 src/core/Cool.pm
  3. +1 −1 src/core/Duration.pm
  4. +1 −1 src/core/Instant.pm
  5. +1 −1 src/core/Int.pm
  6. +4 −5 src/core/Num.pm
  7. +5 −3 src/core/Numeric.pm
  8. +3 −4 src/core/Rat.pm
  9. +18 −6 src/core/Real.pm
  10. +1 −1 t/spectest.data
@@ -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 { ... };
@@ -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);
@@ -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;
View
@@ -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) }
View
@@ -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.
View
@@ -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
View
@@ -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(
View
@@ -1,5 +1,4 @@
-
-my class Num {
+my class Num does Real {
multi method WHICH(Num:D:) {
nqp::box_s(
nqp::concat_s(
@@ -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) {
View
@@ -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 }
@@ -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 }
View
@@ -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;
@@ -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
View
@@ -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 }
@@ -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
}
@@ -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(|$) {*}
@@ -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) {
@@ -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) }
View
@@ -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

0 comments on commit 4157918

Please sign in to comment.