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.

39 comments:

Unknown said...

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

Sindh AI Research Lab said...

How to load notMNIST on keras library

Anonymous said...

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

Unknown said...

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.

Unknown said...

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.

Unknown said...

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.

Unknown said...

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.

linda said...

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.

Unknown said...

Thank you... training institute in Jaipur

Anonymous said...

Nice Arcticle...
Machine Learning Interview Questions and Answers
Artificial Intelligence Interview Questions and Answers

Avanexa Technologies said...

great blog...


BEST DIGITAL MARKETING AND WEBSITE DESIGNING COMPANY IN COIMBATORE

Machine Learning Training in Bangalore | Machine Learning course - ICSS said...

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/

unknow said...

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>

mobile application development said...

Amazing Post. Your blog is very inspiring. Thanks for Posting.
Mobile App Development Company in chennai
mobile app development chennai
Mobile application development company in chennai
Mobile application development chennai
Mobile apps development companies in chennai
enterprise mobile app development company

Elevators and Lifts said...
This comment has been removed by the author.
Elevators and Lifts said...

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

Angular expert said...

Very nice post. I absolutely love this site. Thanks!



Angular Training in Bangalore

Python Training in Marathahalli, Bangalore

Angular expert said...

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

deepika said...

very nice article and very informative information.
jquery interview questions

iuwekjhbr said...

Please refer below if you are looking for Online Job Support and Proxy support from India


Java Online Job Support and Proxy support from India | AWS Online Job Support and Proxy Support From India | Python Online Job Support and Proxy Support From India | Angular Online Job Support from India | Android Online Job Support and Proxy Support from India

Thank you for excellent article.

Anonymous said...


Hello, o you know that the best way to boost your brain is
visiting or contacting us


https://weiiitrading.com/our-products/moonrock-carts/buy-empty-moonrock-clear-vape-cartridges-blue-carts-dr-zodiak-atomizers-with-flavor-box-packaging/

https://weiiitrading.com/our-products/heavy-hitters-carts/buy-wholesale-new-heavy-hitter-vape-cartridges-1-0ml-ceramic-coil-empty-tank-carts-510-thread-thick-oil-atomizer/

https://weiiitrading.com/our-products/juul-carts/buy-hot-empty-ceramic-pod-disassembled-cartridges-0-7ml-1-0ml-vape-pod-carts-for-vape-juul-vape-pen-start-kit-top-quality/

https://weiiitrading.com/our-products/mario-carts/buy-peaches-and-dream/

https://weiiitrading.com/our-products/heavy-hitters-carts/buy-bubba-kush-cartridge-2-2g/

https://weiiitrading.com/our-products/mario-carts/buy-thin-mint-cookies/



Pila Brass Knuckles Online for sale online,

where to buy Buy Pila Brass Knuckles Online

buy valley online cartridge

buy space candy online

buy cannabis syrup online

buy botox online

cannabis bread

uk chese

47 dank vapewhite fire og

buy moonrock



Email Us

Contact: +1 619-537-6734

Cloudi5 said...

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

Sudharshan said...

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/

Digital Marketing in Coimbatore said...

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

Offshore Web Development Company India said...

This is awesome information, Thank you for this!

pranay said...

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!

Trendycollectionskollam said...

NICE INFORMATION,GREAT POST.
Click here: trendy dress in India
Top shoes in India
popular silk sarees
trendy jwellery

Penta Techsoft Solution said...

Interesting blog great!

Website Designing | Digital Marketing

best it company in surat said...

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

Thx a lot !!

here you go : best it company in surat

Aaradhana Technology said...

cms development company in india

best web design company in chennai

website design in coimbatore

best web design company in trichy

web designing in thanjavur

outsource video editing services

Aswin said...

We are the leading website development company in coimbatore . For further info website development company in coimbatore

periyannan said...

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

MindMade-technologies said...

Hi

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

veera04 said...

Thank you.....

WorkDay Training

Mukesh said...

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

pega training in hyderabad

digital marketing coimbatore said...

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

graphic design company in Coimbatore said...

Good article. Such a valuable information provided here.

digital branding company in coimbatore
best web design company in coimbatore

Lourdes Multi Speciality Hospital in Kochi said...

Lourdes hospital is the best Cancer treatment hospitals in Kerala

KEM Builders & Developers said...

KEM Builders offers Builders in Kizhakkambalam