.NET 2003 subtract precision of double & float

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi there

I'm using the Visual Studio .NET 2003 -> C#.

If i try to subtract 145.05 - 140.0 i will get 5.050000000001.
Why isn't the result 5.05?

My testcode is:

double a = 145.05;
double b = 140;
double c = 0;

c = a - b;

this.txtResult.Text = c.ToString();

I thought that could be a failure of the conversion of the ToString method, so i used the debugger to find out where the problem is, but the var "c" is 5.05000000001.

Does anybody know how i can fix this?
Could that be a failure of my processor (Intel P4 HyperThreading), or is that a failure of the .NET Framework?

I hope someone can help me to fix this.

Thanks a lot & Best Regards
Clever & Smart
 
Clever & Smart,
Could that be a failure of my processor (Intel P4 HyperThreading), or is
that a failure of the .NET Framework?

Its actually a "failure" of the way floating point numbers work. I don't
specifically consider it a failure as just "that's the way it works".

For details see:
http://www.yoda.arachsys.com/csharp/floatingpoint.html
Does anybody know how i can fix this?
Not a fix per se, however you can consider using Decimal.
http://www.yoda.arachsys.com/csharp/decimal.html

Hope this helps
Jay

Clever & Smart said:
Hi there

I'm using the Visual Studio .NET 2003 -> C#.

If i try to subtract 145.05 - 140.0 i will get 5.050000000001.
Why isn't the result 5.05?

My testcode is:

double a = 145.05;
double b = 140;
double c = 0;

c = a - b;

this.txtResult.Text = c.ToString();

I thought that could be a failure of the conversion of the ToString
method, so i used the debugger to find out where the problem is, but the var
"c" is 5.05000000001.
Does anybody know how i can fix this?
Could that be a failure of my processor (Intel P4 HyperThreading), or is
that a failure of the .NET Framework?
 
Jay B. Harlow said:
Clever & Smart,
that a failure of the .NET Framework?

Its actually a "failure" of the way floating point numbers work. I don't
specifically consider it a failure as just "that's the way it works".

For details see:
http://www.yoda.arachsys.com/csharp/floatingpoint.html

Not a fix per se, however you can consider using Decimal.
http://www.yoda.arachsys.com/csharp/decimal.html

Darn it Jay, with you posting the links I was going to post, how am *I*
meant to respond to the post?

;)
 
Jon,
Well if you wouldn't write such darn good articles on the subjects. ;-)

Jay
 
Hi Jay

Thx a lot for your fast reply.
I've read this page about floating points. Sounds interesting, but what i didn't understand is: why does it only happens, if the number is 0.04 or 0.05? And if i calculate with 0.005 this problem doesn't happen. Only by values 0.04 and 0.05.

A few years ago, i wrote some "Visual C++ Console" / "Linux C++" / "Java" applications (currency converter), in this applications i worked with double's too, but then i hadn't this problems.
I'm very sorry about my "distrust" :-(

Thanks and best regards
Clever & Smart


Jay B. Harlow said:
Jon,
Well if you wouldn't write such darn good articles on the subjects. ;-)

Jay
 
Clever & Smart said:
Thx a lot for your fast reply.
I've read this page about floating points. Sounds interesting, but
what i didn't understand is: why does it only happens, if the number
is 0.04 or 0.05? And if i calculate with 0.005 this problem doesn't
happen. Only by values 0.04 and 0.05.

It'll still be happening (unless you're very lucky in what exact
numbers you're using), just in a way which isn't visible with the
default precision used when converting the number to string.

If you look at the exact value represented by the doubles you're using,
with something like
http://www.pobox.com/~skeet/csharp/DoubleConverter.cs
you'll see what I mean.
A few years ago, i wrote some "Visual C++ Console" / "Linux C++" /
"Java" applications (currency converter), in this applications i
worked with double's too, but then i hadn't this problems.

Yes you did, you just didn't notice it - presumably because when you
were looking at the numbers, you were looking at a rounded version of
them which disguised the inaccuracy. There's nothing special about .NET
here.
 
Back
Top