double operation

  • Thread starter Thread starter IceMan
  • Start date Start date
I

IceMan

Hello

I am getting some odd answers when i use doubles in c#
0.55-0.5 = 0.050000000000000044

why am i not getting 0.05
 
IceMan said:
I am getting some odd answers when i use doubles in c#
0.55-0.5 = 0.050000000000000044

why am i not getting 0.05

There are some numbers that contain a finite number of decimals when
encoded in Base 10, but which contain infinite decimal digits when converted
into Base 2. The Double datatype stores the binary representation of the
number, and can only contain a limited number of bits (fifty-something bits,
actually). Since the double can only store a finite number of bits, when you
try to store a number whose base-2 representation has infinite bits, the
stream of bits is truncated and only the first bits are kept. If you convert
such a number back into base 10, it will not be exactly the same number that
you tried to store initially. This is why floats and doubles are called
"imprecise" storage (they are not imprecise in base 2, they are just
imprecise representations of base 10 numbers). If you need a perfectly
accurate storage of your decimal numbers, use the type Decimal instead of
Double.
 
Hello IceMan,

read Jon's article which explains double operation in .NET http://www.yoda.arachsys.com/csharp/floatingpoint.html

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo


I> Hello
I>
I> I am getting some odd answers when i use doubles in c# 0.55-0.5 =
I> 0.050000000000000044
I>
I> why am i not getting 0.05
I>
 
Hello

I am getting some odd answers when i use doubles in c#
0.55-0.5 = 0.050000000000000044

why am i not getting 0.05

Any "real" number in computers uses mantissa and exponent, an error in
representig the real world, 1/3 is 0.3 and a lot of 3:s after that, so
somewhere it has to fail/cut in order to store the value.

as usual in math the nice thing is to know how many decimals are of
intrest, when calculators came in use an answer in a mathtest would be
with only the right number of decimals, so the computer is not passing
the mathtest - neither did my calculator, but u get 0.05 (correct) and
some...

Some funny things appear reading about this is how to handle 0, and is
there a -0?

//CY
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top