September Morn Update

This commit is contained in:
Ingy döt Net 2019-09-12 10:33:56 -07:00
parent 4e2d22a71d
commit aac6731f2c
6856 changed files with 141342 additions and 21127 deletions

View file

@ -0,0 +1,43 @@
#include <iostream>
#include <tuple>
union conv {
int i;
float f;
};
float nextUp(float d) {
if (isnan(d) || d == -INFINITY || d == INFINITY) return d;
if (d == 0.0) return FLT_EPSILON;
conv c;
c.f = d;
c.i++;
return c.f;
}
float nextDown(float d) {
if (isnan(d) || d == -INFINITY || d == INFINITY) return d;
if (d == 0.0) return -FLT_EPSILON;
conv c;
c.f = d;
c.i--;
return c.f;
}
auto safeAdd(float a, float b) {
return std::make_tuple(nextDown(a + b), nextUp(a + b));
}
int main() {
float a = 1.20f;
float b = 0.03f;
auto result = safeAdd(a, b);
printf("(%f + %f) is in the range (%0.16f, %0.16f)\n", a, b, std::get<0>(result), std::get<1>(result));
return 0;
}

View file

@ -0,0 +1,28 @@
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
}

View file

@ -0,0 +1,11 @@
use strict;
use warnings;
use Data::IEEE754::Tools <nextUp nextDown>;
sub safe_add {
my($a,$b) = @_;
my $c = $a + $b;
return $c, nextDown($c), nextUp($c)
}
printf "%.17f (%.17f, %.17f)\n", safe_add (1/9,1/7);