Floating point representation issues

  • Thread starter abhishek ghuwalewala
  • Start date
A

abhishek ghuwalewala

The run-time is returning weird values when I do some floating point
arithmetic. For example, in the VS .NET debugger, if I try to add 0.2 (local
variable of type double) to 0.1 (local variable of type double) I get
0.300000000000000004 which messes up my other calculations. The Epsilon
trick described in the documentation for Double.Epsilon does not work
either. Is there an elegant way to work around this?

Abhishek
 
J

Jon Skeet [C# MVP]

abhishek ghuwalewala said:
The run-time is returning weird values when I do some floating point
arithmetic. For example, in the VS .NET debugger, if I try to add 0.2 (local
variable of type double) to 0.1 (local variable of type double) I get
0.300000000000000004 which messes up my other calculations.

That isn't weird at all... see later.
The Epsilon
trick described in the documentation for Double.Epsilon does not work
either. Is there an elegant way to work around this?

See http://www.pobox.com/~skeet/csharp/floatingpoint.html
 

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

Top