Has anyone actually tested Decimal vs. Double. The reason I ask is that
wrote in message news:eWlNR30pGHA.4760@TK2MSFTNGP05.phx.gbl...
> Owen wrote:
> > Hi all,
> > Wondering if someone can help me with this:
> >
> > when i'm in Visual Studio 2005 I get a wierd addition error when
> > adding
> > 0.1+0.2 (both as doubles)
> >
> > the result that comes back is 0.30000000000000004 not 0.3 as
> > expected. Any ideas what's causing this or what's wrong?
>
> Your expectations are wrong.
>
> The numbers 0.1 and 0.2 do not have exact representations in binary
floating
> point - which is what .NET uses for floats and doubles. Consequently,
when
> you add a number very close to 0.1 to a number very close to 0.2, the best
> you can hope for is a number very close to 0.3. Four double, which you're
> apparently using here, you can expect about 17 digits of accuracy.
>
> When comparing floating point numbers for "equality", you should always
> compare for equality wiithin some small range, e.g. instead of x == y, use
> Math.Abs(x-y) < 0.0000000001.
>
> When displaying floating point numbers, you should always specify how many
> digits after the decimal point, according to your needs.
>
> If you need to be able to add 0.1 to 0.2 and get exactly 0.3 (e.g. you're
> doing financial calculations), then you should use the System.Decimal type
> instead. It is a decimal floating point number and can represent negative
> powers of 10 (e.g. 0.1) exactly. Be aware though - decimal is orders of
> magnitude slower than double, so unless you really need absolute decimal
> accuracy, you're probably better off using double.
>
> You should also take a look at the paper at the following url. It goes
into
> the details of working with binary floating point numbers in great detail.
>
>
http://docs.sun.com/source/806-3568/ncg_goldberg.html >
> -cd
>
>