0

I am facing weird issue. I know double and float are fundamentally different. But for value like 11.9 also it is getting changed. I have by pass the issue by changed the type everywhere but I just want to know the reason why it is getting changed.

Here is snippet code

float sf = float.Parse ("11.9");
double sdo = double.Parse ("11.9");
double sd2 = (double)sf;
Console.WriteLine (sdo); // 11.9
Console.WriteLine (sd2); // 11.8999999999

Please let me know if any further information needed.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
kunjee
  • 2,739
  • 1
  • 23
  • 38
  • `double` and `float` are more the same than different. They are both floating point numeric types, just `double` is twice the size of `float`. – Adam Houldsworth Jun 03 '13 at 11:12
  • @AdamHouldsworth thats why I am doing type casting without worrying but now this put me into problem and I have to revert everything to one type. – kunjee Jun 03 '13 at 11:16
  • @TimSchmelter that conversation I just put to mimic my code. Real issue is conversation from float to double. Even if you remove string part issue will be there. – kunjee Jun 03 '13 at 11:19
  • This conversation has happened many times. The bottom line here is a lack of understanding about how float and double exist in memory/storage. Here's some good info: http://stackoverflow.com/questions/618535/what-is-the-difference-between-decimal-float-and-double-in-c. And: http://www.google.com/url?sa=t&rct=j&q=&esrc=s&frm=1&source=web&cd=22&cad=rja&ved=0CDQQFjABOBQ&url=http%3A%2F%2Fdocs.oracle.com%2Fcd%2FE19957-01%2F806-3568%2Fncg_goldberg.html&ei=o3ysUb9arLrIAajfgZAF&usg=AFQjCNHSL-lclWJrAUgNWVbQ6BAvZRoP9Q&sig2=GKunCrL7nY1W77msKbKwMA&bvm=bv.47244034,d.aWc – DonBoitnott Jun 03 '13 at 11:22
  • float and double have simillar representation (floating point in binary format), but still - they are different, have different precission so inner representation of the same value may be different. I don't see that as a problem - you can never assume precise value of float or double. `11.899999999` is still very close to `11.9` so the value is correct in binary dloating points term. Why is that a problem in your code? – Jarek Jun 03 '13 at 11:23
  • @Pako business requirement. It should not changed. – kunjee Jun 03 '13 at 11:29
  • 2
    Then Floating Point types are the wrong decision. If you need absolut precision then take the decimal type. – Ralf Jun 03 '13 at 11:30
  • Well, it will change, depending on numbers. You may switch to `decimal` type to get precise decimal values instead of using `float` or `double` types. Or for user display, you may round those numbers, depends on what exactly do you need. If it's financial calculation - go with decimals. If it's physics or some other inprecise calculations - stick to double, but it will change - that's how it always been and will be. – Jarek Jun 03 '13 at 11:32
  • @Ralf I already took decimal only. But I was trying to understand that conversion. If it want precision than do it like 11.9000000 instead of 11.8999999. – kunjee Jun 03 '13 at 13:00
  • @Pako changed to decimal. Thanks. – kunjee Jun 03 '13 at 13:00

0 Answers0