RosettaCodeData/Task/Safe-addition/Perl-6/safe-addition.pl6
2019-09-12 10:33:56 -07:00

28 lines
910 B
Raku

say "Floating points: (Nums)";
say "Error: " ~ (2**-53).Num;
sub infix:<±+> (Num $a, Num $b) {
my = (2**-53).Num;
$a - ε + $b, $a + ε + $b,
}
printf "%4.16f .. %4.16f\n", (1.14e0 ±+ 2e3);
say "\nRationals:";
say ".1 + .2 is exactly equal to .3: ", .1 + .2 === .3;
say "\nLarge denominators require explicit coercion to FatRats:";
say "Sum of inverses of the first 500 natural numbers:";
my $sum = sum (1..500).map: { FatRat.new(1,$_) };
say $sum;
say $sum.nude;
{
say "\nRat stringification may not show full precision for terminating fractions by default.";
say "Use a module to get full precision.";
use Rat::Precise; # module loading is scoped to the enclosing block
my $rat = 1.5**63;
say "\nPerl 6 default stringification for 1.5**63:\n" ~ $rat; # standard stringification
say "\nRat::Precise stringification for 1.5**63:\n" ~$rat.precise; # full precision
}