Skip to main content

The Fun of Floating Point Numbers in one Image

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

svick said…
> .NET keeps 9 digits of precision internally, but typically only displays 7 digits of precision

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".
Sandro Magi said…
What are you claiming is not true exactly? Because if you're disputing the claim that you quoted, then maybe you should take it up with Microsoft whose official documentation says:

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.