Accurate Math

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

Accurate Math

Postby mkluwe » Sun Apr 06, 2008 2:42 am


In my opinion the section "Accurate Math" ( 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,
Posts: 1

Re: Accurate Math

Postby Charles » Sun Apr 06, 2008 10:07 am

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
>>> assert 1.0 == .1+.1+.1+.1+.1+.1+.1+.1+.1+.1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>

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.
Posts: 2513
Location: Los Angeles, CA

Return to Discussion

Who is online

Users browsing this forum: No registered users and 10 guests