On 9/12/12 9:14 AM, P Purkayastha wrote:
Can anyone point out what is wrong with this comparison:
sage: 0.2 + 0.1 == 0.3
False
It seems baffling that something as simple as this returns False.
But it's not equal (in standard 53-bit floating point arithmetic):
sage: (0.2+0.1)-0.3
5.55111512312578e-17
If you want exact computations, you need to use exact numbers, like:
sage: (2/10+1/10)-3/10
0
The problem is that there either round-off error, or that 0.2 and 0.1
and 0.3 cannot be represented exactly in base-2 53-bit floating point
arithmetic.
For interesting reading, see the first couple of chapters of standard
numerical analysis texts, or see http://floating-point-gui.de/, which
addresses exactly your question.
See also
http://artsci.drake.edu/grout/doku.php/teaching_resources#numerical_analysis
for more resources...
Thanks,
Jason
--
You received this message because you are subscribed to the Google Groups
"sage-support" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
Visit this group at http://groups.google.com/group/sage-support?hl=en.