J
Johnny Lee
Hi,
Pls take a look at this code:
Pls take a look at this code:
Johnny said:1130748744.461 1130748744.500
0.039000034332275391
Why are there so many nonsense tails? thanks for your help.
Why are there so many nonsense tails? thanks for your help.
Why are there so many nonsense tails? thanks for your help.
Johnny Lee enlightened us with:
Because if the same reason you can't write 1/3 in decimal:
http://docs.python.org/tut/node16.html
Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
I think that the previous poster was asking something different.
If
0.039
Then why:
0.0389995574951
Using decimal as opposed to float sorts out this error as floats are
not built to handle the size of number used here.
Ben O'Steen enlightened us with:
They can handle the size just fine. What they can't handle is 1/1000th
precision when using numbers in the order of 1e10.
Ben said:I used the word 'size' here incorrectly, I intended to mean 'length'
rather than numerical value. Sorry for the confusion
Steve said:Sybren is right. The problem is not the length or the size, it's
the fact that 0.039 cannot be represented exactly in binary, in
just the same way that 1/3 cannot be represented exactly in
decimal. They both give recurring numbers. If you truncate those
recurring numbers to a finite number of digits, you lose
precision. And this shows up when you convert the inaccurate
number from binary into decimal representation where an exact
representation IS possible.
Dan said:That's A source of error, but it's only part of the story. The
double-precision binary representation of 0.039 is 5620492334958379 *
2**(-57), which is in error by 1/18014398509481984000. By contrast,
Johnny Lee's answer is in error by 9/262144000, which is more than 618
billion times the error of simply representing 0.039 in floating point
-- a loss of 39 bits.
The problem here is catastrophic cancellation.
1130748744.500 ~= 4742703982051328 * 2**(-22)
1130748744.461 ~= 4742703981887750 * 2**(-22)
Subtracting gives 163578 * 2**(-22), which has only 18 significant bits.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.