SgDotNet
Singapore Professional .NET User Group -For Cool Developers

Difference between += and +

Latest post 05-18-2005 3:36 PM by hannes. 1 replies.
  • 05-18-2005 10:12 AM

    • tom888
    • Top 200 Contributor
    • Joined on 03-18-2005
    • Posts 8

    Difference between += and +

    The difference with "+= Operant" and "+ Operant", are both function the same?

    Code:

    Dim dbl_A As Double =  29.9

    Dim dbl_B As Double = 49.5

    Dim dbl_C As Double = 7.425

    Result A (86.824999999999989)

    dbl_A += dbl_B + dbl_C

    Result B (86.825)

    dbl_A = dbl_A + dbl_B + dbl_C

  • 05-18-2005 3:36 PM In reply to

    • hannes
    • Top 25 Contributor
    • Joined on 05-04-2004
    • South Africa
    • Posts 240

    Re: Difference between += and +

    You can not rely on the answer being exactly 86.825 using doubles. Depending on the numbers being added, the answer may or may not be precise, so you need to perform rounding to X decimal places.

    The example just happens to be precise in the first case because of  the order in which B, C, and A are added. But you can't know this beforehand.

    If you want a precise answer, try the decimal data type.

Page 1 of 1 (2 items) | RSS
Copyright SgDotNet 2004-2008
Powered by Community Server (Commercial Edition), by Telligent Systems