Skip to content

Instantly share code, notes, and snippets.

@Ovid
Created February 9, 2016 09:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ovid/74ffc49d1b8450036e80 to your computer and use it in GitHub Desktop.
Save Ovid/74ffc49d1b8450036e80 to your computer and use it in GitHub Desktop.
Perl 6 Rats throw an exception when trying to get too precise?
# per https://perso.ens-lyon.fr/jean-michel.muller/chapitre1.pdf (page 8), the
# following should approach the limit of 6, but all floating point implementations
# go crazy and approach a limit of 100. I wrote it in Perl 6 using Real and sure enough,
# it approaches 6 before going crazy and approaching 100. But when I switch from Real to
# Rat, I get "Type check failed in assignment to $w; expected Rat but got Num" after
# 25 iterations
# This is Rakudo version 2015.12-307-gd68c304 built on MoarVM version 2016.01
# implementing Perl 6.c.
# https://perso.ens-lyon.fr/jean-michel.muller/chapitre1.pdf
use v6;
my Rat ( $u, $v ) = 2.0, -4.0;
for 3 .. 1000 -> $n {
my Rat $w = 111.0 - 1130.0 / $v + 3000.0 / ( $v * $u );
( $u, $v ) = $v, $w;
printf("u%d = %1.17g\n", $n, $v);
}
@thundergnat
Copy link

my @limit = 2.FatRat, (-4).FatRat, { 111 - 1130 / $^v + 3000 / ( $^v * $^u ) } ... *;
printf("u%d = %1.17g\n", $++, $_ ) for @limit[^200];

@thundergnat
Copy link

Also referenced in that pdf, Rump’s example, Perl 6 doesn't do too bad without any particular effort.

sub f (\a, \b) { 333.75*b⁶ + a²*( 11*a²*b² - b⁶ - 121*b⁴ - 2) + 5.5*b⁸ + a/(2*b) }
say f(77617.0, 33096.0 ).fmt("%0.18g");    # -0.827396059946821

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment