Created
February 13, 2012 18:17
-
-
Save moritz/1818785 to your computer and use it in GitHub Desktop.
Experiments with dividing really big integers into Num
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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