Remix.run Logo
quietbritishjim 3 days ago

When writing code, it's useful to follow simpler rules than strictly needed to make it easier to read for other coders (including you in the future). For example, you might add some unnecessary brackets to expressions to avoid remembering all the operator precedence rules.

The article is right that sometimes you can can use simple equality on floating point values. But if you have a (almost) blanket rule to use fuzzy comparison, or (even better) just avoid any sort if equality-like comparison altogether, then your code might be simpler to understand and you might be less likely to make a mistake about when you really can safely do that.

It's still sensible to understand that it's possible though. Just that you should try to avoid writing tricky code that depends on it.

kccqzy 2 days ago | parent | next [-]

Every time I use exact floating point equality I simply write a comment explaining why I did this. That's what comments are for.

zokier 3 days ago | parent | prev [-]

That sort of argument can be problematic though, because the code can become misleading. If I see a fuzzy comparison then I'd assume it is there because it is needed, which might make the rest of the code more difficult to understand/modify because I then have to assume that everywhere else the values might be fuzzy.