in reply to Floating point (in)accuracy: more "interesting" than expected
# dwhite@nist.gov 3/23/04 - based on BASIC code # in "Astronomical Algorithms" 2nd ed. by Jean Meeus, # pg 17-19 # start of code block 1 $x = 1 ; $j = 0 ; $x *= 2 ; print "Testing significant bits, significant digits...\n"; while (($x + 1) != $x) { print "\t$j\t$x\n"; $j++ ; $x *= 2 ; } print "\t$j\t$x\n"; print "\n", $j , " significant bits in mantissa of floating number\n" +, int($j * 0.30103) , " significant digits in a decimal number (", $j + * 0.30103 , ")\n above is only for SIMPLE ARITHMETICS, not trig func +tions!\n"; # end of code block 1 print "\nthe 2nd column here should NOT list diverging numbers...\n"; print "\$x=1.0/3.0; for(\$j=1;\$j<31;\$j++) { \$x = (9*\$x+1)*\$x-1 ; }\n"; # start of code block 2 $x = 1.0/3.0 ; for($j=1;$j<31;$j++) { $x = (9*$x+1)*$x-1 ; # print "$j\t$x\n"; # for details if ($j % 6 == 1) { print "$j\t$x\n"; } } print "However, they probably will diverge on your machine.\n"; # end of code block 2
|
|---|