Skip to content
Permalink
Browse files

Update Rational.pm6 in response to review comments

  • Loading branch information...
thundergnat committed Mar 27, 2019
1 parent d4e1d8e commit b69964b50eca9ef7c4ddffebbc06022df459438c
Showing with 16 additions and 11 deletions.
  1. +16 −11 src/core/Rational.pm6
@@ -189,13 +189,6 @@ my role Rational[::NuT = Int, ::DeT = ::("NuT")] does Real {
2 <= $base <= 36 or fail X::OutOfRange.new(
what => "base argument to base", :got($base), :range<2..36>);

my $sign = nqp::if( nqp::islt_I($!numerator, 0), '-', '' );
my $whole = self.abs.floor;
my $fract = self.abs - $whole;

# fight floating point noise issues RT#126016
if $fract.Num == 1e0 { $whole++; $fract = 0 }

my $prec;
if $digits ~~ Whatever {
$digits = Nil;
@@ -217,18 +210,30 @@ my role Rational[::NuT = Int, ::DeT = ::("NuT")] does Real {
}
}
else {
# Limit log calculation to 10**307 or less.
# log coerces to Num. When larger than 10**307, it overflows and
# returns Inf.
my $lim = 10**307;
if $!denominator < $lim {
$prec = ($!denominator < $base**6 ?? 6 !! $!denominator.log($base).ceiling + 1);
}
else {
my $f = $!denominator;
my $exp2 = 0;
++$exp2 while ($f div= $base) > $lim;
$prec = $exp2 + $f.log($base).ceiling + 2;
# If the internal log method is modified to handle larger numbers,
# this branch can be modified/removed.
my $d = $!denominator;
my $exp = 0;
++$exp while ($d div= $base) > $lim;
$prec = $exp + $d.log($base).ceiling + 2;
}
}

my $sign = nqp::if( nqp::islt_I($!numerator, 0), '-', '' );
my $whole = self.abs.floor;
my $fract = self.abs - $whole;

# fight floating point noise issues RT#126016
if $fract.Num == 1e0 { $whole++; $fract = 0 }

my $result = $sign ~ $whole.base($base);
my @conversion := <0 1 2 3 4 5 6 7 8 9
A B C D E F G H I J

0 comments on commit b69964b

Please sign in to comment.
You can’t perform that action at this time.