Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Try to clean up and make consistent Real.Int, Real.Rat, and Real.Num.
  • Loading branch information
colomon committed May 19, 2010
1 parent 603c64c commit c6ecc98
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 deletions.
8 changes: 5 additions & 3 deletions src/core/Int.pm
Expand Up @@ -28,10 +28,12 @@ augment class Int does Real {

our Bool multi method Bool() { self != 0 ?? Bool::True !! Bool::False }

our Int multi method Int() { self }
our Int method Int() { self; }

our Num multi method Num() {
pir::box__PN(pir::set__NP(self))
our Int method Rat(Real $epsilon = 1.0e-6) { self / 1; }

our Num method Num() {
pir::box__PN(pir::set__NP(self));
}

# Next has been moved to Rat.pm for the moment.
Expand Down
12 changes: 4 additions & 8 deletions src/core/Num.pm
Expand Up @@ -25,7 +25,7 @@ augment class Num does Real {
self != 0.0e0
}

multi method Int() {
method Int() {
Q:PIR {
$P0 = find_lex 'self'
$I0 = $P0
Expand All @@ -35,11 +35,9 @@ augment class Num does Real {
}
}
multi method Num() { self; }
method Rat(Real $epsilon = 1.0e-6) {
my sub modf($num) { my $q = $num.Int; $num - $q, $q; }
method !modf($num) { my $q = $num.Int; $num - $q, $q; }
multi method Rat($epsilon = 1.0e-6) {
my $num = +self;
my $signum = $num < 0 ?? -1 !! 1;
$num = -$num if $signum == -1;
Expand All @@ -65,9 +63,7 @@ augment class Num does Real {
($signum * $b) / $d;
}
# multi method exp() {
# pir::exp__Nn(self);
# }
method Num() { self; }
method ln(Num $x:) {
pir::ln__Nn($x);
Expand Down
15 changes: 5 additions & 10 deletions src/core/Rat.pm
Expand Up @@ -32,15 +32,15 @@ class Rat is Cool does Real {

our Bool multi method Bool() { $!numerator != 0 ?? Bool::True !! Bool::False }

multi method Num() {
method Int() { self.Num.Int; }

method Rat(Real $epsilon = 1.0e-6) { self; }

method Num() {
$!denominator == 0 ?? Inf * $!numerator.sign
!! $!numerator.Num / $!denominator.Num;
}

multi method Rat() { self; }

multi method Int() { self.Num.Int; }

multi method Str() { $.Num.Str; }

multi method nude() { $.numerator, $.denominator; }
Expand Down Expand Up @@ -114,9 +114,4 @@ multi sub infix:</>(Int $a, Int $b) {
Rat.new($a, $b);
}

augment class Int {
# CHEAT: Comes from Int.pm, moved here for the moment.
our Rat multi method Rat() { Rat.new(self, 1); }
}

# vim: ft=perl6 sw=4 ts=4 expandtab
12 changes: 12 additions & 0 deletions src/core/Real.pm
Expand Up @@ -5,6 +5,18 @@ role Real does Numeric {
fail "Bridge must be defined for the Real type " ~ self.WHAT;
}

method Int() {
self.Bridge.Int;
}

method Rat(Real $epsilon = 1.0e-6) {
self.Bridge.Rat($epsilon);
}

method Num() {
self.Bridge.Num;
}

method Complex() {
Complex.new(self, 0);
}
Expand Down

0 comments on commit c6ecc98

Please sign in to comment.