Skip to content

Comment on Floats Don't Agree with Themselves

Comments

That's why they always teach: "never compare floats for equality."

Or maybe they don't teach that anymore, I dunno.

See link for the Fundamental Axiom of Floating Point Arithmetic: All floating point arithmetic operations are exact up to a relative error of epsilon_machine.

https://www.johnbcoughlin.com/posts/floating-point-axiom/

I certainly teach that. When we work problems involving money, I always recommend students use integers for cents and only convert to dollars and cents when they have to print them.

On the other hand, if you store a small integer in a float it is generally reliable to compare to it. E.g., setting a float to zero and comparing whether the float is zero.

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.