.NET Framework Developer Center >
.NET Development Forums
>
Common Language Runtime
>
Wrong double-calculation results
Wrong double-calculation results
- I'm using VB.Net 2008.
In a financial calculation I have two double variables with the values of 13.15 and 2.2.
When I add these two values I got 15.350000000000001 instead of 15.35 !!!
Same behavior in the in the immediate or command window: "? 13.15 + 2.2" gives the wrong value.
When I was programming VB6, I got sometimes the same behavior, someone told me that .Net will never have this problem! Too bad!
Any ideas...?
Answers
- It's not a bug. The way floating points work is normalized across platform and across programming languages. It just works that way. Double isn't meant to be a perfectly accurate value type. It's used quite frequently in rendering, drawing, etc, because those types of applications don't require exact accuracy.
Yes, when you don't specify the value's type "15.22" will be interpreted as a double. You can force the compiler to interpret it as a decimal by using the "M" letter after the number... "15.22M".
Use a decimal for financial calculations, but to answer your question, a double isn't "useless". There are situations when an exactly accurate calculation isn't needed. In those situations, a double should be used.
WPF uses the double type extensively to represent lengths and widths with vector scaling for graphics.
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki • LinkedIn • ForumsBrowser- Marked As Answer byeryangMSFT, ModeratorMonday, November 09, 2009 6:49 AM
All Replies
- Hello,
For financial applications it is highly recommended to use decimal type. This type has much better precision than Double.
On the other hand Double is less-precise, than Decimal, but it is much faster because it is supported at CPU level.
Vitaliy Liptchinsky http://dotnetframeworkplanet.blogspot.com/ - So is this a normal behavior or a bug?
If people want to calculate correctly, a double variable will never usefull. Why did MS add the double to the framework when it's useless?
For the immediate window all variables are understood as doubles? - It's not a bug. The way floating points work is normalized across platform and across programming languages. It just works that way. Double isn't meant to be a perfectly accurate value type. It's used quite frequently in rendering, drawing, etc, because those types of applications don't require exact accuracy.
Yes, when you don't specify the value's type "15.22" will be interpreted as a double. You can force the compiler to interpret it as a decimal by using the "M" letter after the number... "15.22M".
Use a decimal for financial calculations, but to answer your question, a double isn't "useless". There are situations when an exactly accurate calculation isn't needed. In those situations, a double should be used.
WPF uses the double type extensively to represent lengths and widths with vector scaling for graphics.
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki • LinkedIn • ForumsBrowser- Marked As Answer byeryangMSFT, ModeratorMonday, November 09, 2009 6:49 AM


