0.1 cannot be accurately represented in the binary format, and if
so, how come adding 0.1 successively on my computer works?
$PREC=64;
$radix=1;
$target=0.1;
print("0.");
for ($i=0; $i<$PREC; $i++) {
$radix/=2;
if ($radix<=$target) {
$target-=$radix;
print("1");
} else {
print("0");
}
}
print("\n");
\_ have you taken 61c? IEEE-754 floating point doesn't use all 64 bits
for the significand, you know. And no, 0.1 (decimal) can't be
represented exactly in binary. Try it on paper.
\_ the point of the poster is that since it cannot be represented
exactly in binary, why it works on modern processors.
\_ Well we know that certain Intel cpus just ignore all that
precision stuff anyway. :-) |