Saturday, September 24, 2011

Don't test for exact equality of floating point numbers


A discussion came up on Guido von Rossum's Google Plus post. It comes down to the fact that 2.1 is not exactly represented as a floating point number. Internally it's 2.0999999999999996, and this causes unexpected behavior.

These kinds of issues often come up. The confusion is caused by treating floating point numbers as exact numbers, and expecting calculations with them to produce results meaningful down to the last bit.

People at Wolfram Research has spent a lot of effort trying to get arithmetic right, and here's what a principle kernel developer, Daniel Lichtblau, has to say about the issue of making floating point calculations deterministic:

It is not in any way "solvable", at least not by means accessible to us (which in some sense defines it as "not a problem"). Depends too much on alignment-handling vagaries of MKL libraries, ordering of operations in BLAS, and usage, or not, of extended precision registers. See also IEEE 754: a careful reading may shed light on how different results for the same computation can arise in compliant hardware/software, even on the same machine

In addition to ambiguity in IEEE 754, which gives slight differences between different floating point libraries, there's an issue of the same library giving slightly different results on reruns with same inputs and and same machine, because of run-time optimization by the processor

The solution that Wolfram Inc came up with is to treat the last 7 bits of IEEE doubles as unknown. When testing for equality, those bits are ignored. When printing a number, it chooses representation that gives a nice printout. For instance, Print[2.0999999999999996] will display 2.1

So here's the rule of thumb for IEEE 754 floating point numbers:

When checking for equality of floating point doubles, ignore the last 7 bits of the mantissa.

40 comments:

  1. Just in case you haven't seen it yet:
    http://download.oracle.com/docs/cd/E19957-01/806-3568/ncg_goldberg.html

    ReplyDelete
  2. How to load notMNIST on keras library

    ReplyDelete
  3. Anonymous5:34 PM

    got 94.5% accuracy with 3 layers (all with RELU): 1024, 256, 64, applied dropout with prob=0.5

    ReplyDelete
  4. People are really wanted to go with the flows and let have a perfect life while they are floating in nowhere and then this could help them out. https://www.nursingcapstone.org/nursing-student-portfolio/ will give you some amazing idea and very effective for the papers writing.

    ReplyDelete
  5. While you wanted to go with the flow you need to know that not every time you should follow someon and go on. go to site that I love for the helpful information and its really amazing.

    ReplyDelete
  6. People are always waited for this day. Yeah, I am talking about the new year day when people love the day and love to do new thing with new promises. https://www.writemylitreview.com/our-services/ here abut the writing tips.

    ReplyDelete
  7. You cannot just measure with the right equality when the things are really floating. Click here http://www.paschoolrequirements.com/pa-programs-in-maine/ to know about the maine program.

    ReplyDelete
  8. We may have keep the balance on where we are working on but for that every task should be take into account. http://www.personalstatementpsychology.com/counselor-personal-statement-that-will-succeed-8-tips-to-bare-in-mind/ will help you out with some tips.

    ReplyDelete
  9. Wonderful blog!!! the article which you have shared is informative for us.Python has been the top most powerful and flexible open source language that is really very easy to learn. In our instructor based Python Training in Bangalore Advance Level we will teach you how to use the powerful libraries for data analysis and manipulation.https://indiancybersecuritysolutions.com/python-training-in-bangalore-advance-level/

    ReplyDelete
  10. I really enjoyed your blog Thanks for sharing such an informative post.
    https://myseokhazana.com/
    https://seosagar.in/
    Indian Bookmarking list
    Indian Bookmarking list
    India Classified Submission List
    Indian Classified List
    <a href="https://seosagar.in/</a>

    ReplyDelete
  11. This comment has been removed by the author.

    ReplyDelete
  12. Great post. Are you looking for the best Automatic gates in India,
    Then click here: Automatic gates

    ReplyDelete
  13. You need to take part in a contest for one of the finest websites on the net. I will highly recommend this website!

    UI Development Training in Marathahalli

    Full stack Development Training in Marthahalli Bangalore


    UI Development Training in Bangalore

    ReplyDelete
  14. very nice article and very informative information.
    jquery interview questions

    ReplyDelete
  15. Thanks for sharing this stuff. It helps me a lot on while implementing my web design and web development projects.

    ReplyDelete
  16. Great blog !It is best institute.Top Training institute In chennai
    http://chennaitraining.in/openspan-training-in-chennai/
    http://chennaitraining.in/uipath-training-in-chennai/
    http://chennaitraining.in/automation-anywhere-training-in-chennai/
    http://chennaitraining.in/microsoft-azure-training-in-chennai/
    http://chennaitraining.in/workday-training-in-chennai/
    http://chennaitraining.in/vmware-training-in-chennai/

    ReplyDelete
  17. Super Post thanks for valuable post
    https://www.avavaventures.com/contact/

    ReplyDelete
  18. This is awesome information, Thank you for this!

    ReplyDelete
  19. very special. easy to understand…. taking things from root .. stay blessed
    We have an excellent IT courses training institute in Hyderabad. We are offering a number of courses that are very trendy in the IT industry. For further information.
    best machine learning course online!

    ReplyDelete
  20. Loved your explanation towards Maths logic. once again you remembered floating concept.

    Thx a lot !!

    here you go : best it company in surat

    ReplyDelete
  21. Your info is really amazing with impressive content..Excellent blog with informative concept. Really I feel happy to see this useful blog, Thanks for sharing such a nice blog..
    artificial intelligence internship | best final year projects for cse | internship certificate online | internship for mba finance students | internship meaning in tamil

    ReplyDelete
  22. Hi

    Great opportunity to learn this kind of good articles , please do more postings . Thanks for sharing with us . https://www.mindmade.in/

    ReplyDelete
  23. Thanks for sharing these information, I really enjoyed reading these kind of information.

    pega training in hyderabad

    ReplyDelete
  24. Thank you for your post. This is superb information. It is amazing and great to visit your site.

    ReplyDelete
  25. When booking Edinburgh Wedding Hair and Makeup services, brides can expect a consultation to discuss their vision and preferences for their wedding day look. This allows stylists and makeup artists to tailor their services to each individual bride, ensuring that the final result exceeds expectations.

    Whether the bride desires a glamorous, red-carpet-worthy style or a soft, romantic look, wedding hair and makeup Edinburgh professionals have the skills and expertise to bring any vision to life. By choosing Edinburgh Wedding Hair and Makeup services, brides can relax and enjoy the pampering experience of having their hair and makeup done professionally, knowing that they are in capable hands for their wedding day.

    ReplyDelete