0

I know that floating point variable stores the number in a sign-exponent-fraction format (as it's stated in the IEEE 754), it's never precise and I should probably never compare two floats without specifying the precision.

But why exactly 0.09f - 0.01f gives you the value of 0.0800000057f? What exactly happens in under the hood of the .NET VM and in the memory when I do that subtraction?

splattru
  • 608
  • 1
  • 9
  • 19
  • 1
    Have you looked at the *exact* values of 0.09f and 0.01f to start with? See http://csharpindepth.com/Articles/General/FloatingPoint.aspx and find the DoubleConverter code there to see the exact values. Then the subtraction result probably won't be a surprise. – Jon Skeet Nov 04 '15 at 08:06

1 Answers1

2

0.09 is represented as 00111101101110000101000111101100 in ieee-754, which is closest to the decimal value of 0.09000000357627869

0.01 is represented as 00111100001000111101011100001010 in ieee-754, which is closest to the decimal value of 0.009999999776482582

Their subtraction yields 00111101101000111101011100001011 which is closest to the decimal value of 0.08000000566244125

You can see how the actual subtraction is done here

Community
  • 1
  • 1
SWeko
  • 30,434
  • 10
  • 71
  • 106