0

Edit: This is not a duplicate. The question linked does not explain specifically where the 190734863 comes from. I understand the differences in precision. I want to know where the extra data is derived from and specifically the sequence of numbers I have quoted. I have made this clear in my question also from the start of the question.

I have created the following code to implicitly convert from a float to a double.

// Implicitly converting a float to a double. 

            float floatingNumber = 1.23f;
            double doubleNumber;

            doubleNumber = floatingNumber;

When I inspect the locals window during debug, I notice the following values.

float = 1.23   // as expected
double = 1.2300000190734863   // hmm..

My question is where do the 190734863 come from? in the double?

Thankyou!

GaryDev
  • 15
  • 8
  • This is not a duplicate. The question linked does not explain specifically where the 190734863 comes from. I understand the differences in precision. I want to know where the extra data is derived from and specifically the sequence of numbers I have quoted. I have made this clear in my question also from the start of the question. – GaryDev Sep 22 '15 at 12:54
  • Have you investigated what the *exact* value of `floatingNumber` is and what the *exact* value of `doubleNumber` is? Hint: neither of them is 1.23. (But they will be the same...) And no, you haven't made it clear in your question that you understand how floating point works. You might want to read http://csharpindepth.com/Articles/General/FloatingPoint.aspx – Jon Skeet Sep 22 '15 at 12:56
  • @GaryDev The linked question does explain it: `not all decimal numbers are exactly representable in binary floating point – 0.1, for example – so if you use a binary floating point value you'll actually get an approximation to 0.1.` – Rob Sep 22 '15 at 12:59
  • @JonSkeet Thank you for your steer, how do I obtain the exact value, I have obtained the values in my original post from using the locals window. – GaryDev Sep 22 '15 at 13:00
  • @GaryDev Write out the value of `1.23` in binary with the precision of `double`. You won't be able to do it. The difference between what you *are* able to write, and `1.23` will equal `0.0000000190734863` – Rob Sep 22 '15 at 13:01
  • @Rob I see ok thanks for your help. – GaryDev Sep 22 '15 at 13:01
  • @Rob - great thank you I understand now, much appreciated. – GaryDev Sep 22 '15 at 13:02
  • @Rob Actually, the difference is 0.000000019073486328125, since 1.23 converts to a float as 1.230000019073486328125 – Rick Regan Sep 23 '15 at 00:32

0 Answers0