brazzy / floating-point-gui.de

Website that provides concise answers to common questions about floating-point numbers.
http://floating-point-gui.de/
1.01k stars 166 forks source link

NeralyEqual function is inefficient #72

Closed chmike closed 3 years ago

chmike commented 3 years ago

The code to test if floats are nearly equal is not very efficient. It is a problem when the test is to be performed in speed critical application like graphic.

Could there be a more efficient solution ?

chmike commented 3 years ago

It seams that this web document presents a far more efficient solution: https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/

brazzy commented 3 years ago

Yes, the code is absolutely not optimized for performance - if anything it's optimized to show off how many special cases you need to allow for. And if you do need a performance-optimized version than most of all (as I wrote) you need to tailor it to your specific usecase, which may allow you to drop some special cases.

And the far more efficient solution you link to is actually mentioned in the next section ("Comparing floating-point values as integers") with a link to exactly the same article.