Page 1 of 1

Accurate Math

PostPosted: Sun Apr 06, 2008 2:42 am
by mkluwe
Hi!

In my opinion the section "Accurate Math" (http://cobra-language.com/docs/python/) should be changed.

Choosing decimal arithmetic as a default may an important difference in a comparison with python, but the statement »Python performs arithmetic incorrectly« is incorrect. Python correctly performs perfect binary floating point arithmetic for anyone who wants to do that. Many people do...

Kind regards,
Matthias

Re: Accurate Math

PostPosted: Sun Apr 06, 2008 10:07 am
by Charles
Hmm, but it does not say "Out of the box, Python performs *binary* arithmetic incorrectly:"

It says "Out of the box, Python performs arithmetic incorrectly:" which is true for Python, C# and many programming languages. The statement is immediately followed by:
Code: Select all
Python 2.5 (r25:51918, Sep 19 2006, 08:49:13)
>>> .1+.1+.1+.1+.1+.1+.1+.1+.1+.1  # .1 added 10 times should come out to 1.0
0.99999999999999989
>>> assert 1.0 == .1+.1+.1+.1+.1+.1+.1+.1+.1+.1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError

Certainly 0.99999999999999989 is the correct answer from a binary arithmetic perspective, but the wrong answer from an arithmetic perspective. 0.1 added 10 times is 1.0

I've reread this section in Comparison to Python and I'm comfortable with it.

I agree with you that many people do want the binary arithmetic for speed or library compatibility (certainly not for accuracy) and consequently Cobra now has -number:float64 type which changes the default type of literals such as "1.0" and the built-in "number" type to float64. You can also specify float32 and decimal, although decimal is redundant since that's the default.

I will add that note to the section. Thanks for your feedback.