Skip to content

Instantly share code, notes, and snippets.

@moritz
Created February 13, 2012 18:17
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 moritz/1818785 to your computer and use it in GitHub Desktop.
Save moritz/1818785 to your computer and use it in GitHub Desktop.
Experiments with dividing really big integers into Num
sub guess_size(Int $a is copy) {
$a = abs $a;
my $ref = 2**16;
my $c = 0;
while $a > $ref {
$a div= $ref;
$c += 16
}
$c;
}
sub DIVIDE(Int $a, Int $b) {
my Int $sa = guess_size $a;
my Int $sb = guess_size $b;
my Int $max_size = $sa max $sb;
# the exponent of a 64bit integer goes up to 1023
# and guess_size underestimates by at most 15 bit
if $max_size > 1008 {
my Int $div = 2**($max_size - 1008);
return ($a div $div).Num / ($b div $div).Num;
}
$a.Num / $b.Num;
}
say DIVIDE(203**200, 200**200) - (203e0 / 200e0)**200;
# output: 3.76587649952853e-13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment