Ask a questionAsk a question
 

AnswerWrong double-calculation results

  • Monday, November 02, 2009 12:41 PMPatrick Simons Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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

  • Tuesday, November 03, 2009 1:47 PMDavid M MortonMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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 WikiLinkedInForumsBrowser

All Replies

  • Monday, November 02, 2009 12:54 PMVitaliy Liptchinsky Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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/
  • Tuesday, November 03, 2009 1:43 PMPatrick Simons Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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?
  • Tuesday, November 03, 2009 1:47 PMDavid M MortonMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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 WikiLinkedInForumsBrowser