Programming with floating point is always fun. Here's a nice little screen capture summarizing the insanity that sometimes arises:
.NET keeps 9 digits of precision internally, but typically only displays 7 digits of precision, so I had a hell of a time figuring out why a value from what's effectively a no-op was exceeding the 0.33F threshold I was looking for.
Losing equational reasoning is always fun, but this is even more bizarre than usual. Yay floating point!
Comments
That's not true on .Net Core/.Net 5+. When I run `(0.33F + 1F - 1F).ToString()` there, the output I get is "0.33000004".
All floating-point numbers have a limited number of significant digits, which also determines how accurately a floating-point value approximates a real number. A Single value has up to 7 decimal digits of precision, although a maximum of 9 digits is maintained internally.
If you're claiming that the expression that I quoted produces different results on your runtime, then great, you just proved my point again: that floating point is a quagmire because different optimizations and compilations of floating point code can produce different results.