Floating Point Calculation Problem

G

Guest

Hello,

In an application that worked fine until recently I have started to have
problems with calculations involving two doubles. For example: -24.708 +
26.0 = 1.2920000000000016, however, -24.708 + 8.0 = -16.708. What would
cause this? I don't think the numbers are so large that it would be the
usual floating point calculation error because of rounding. How can I fix
it? I'd rather not change every calcuation in my program to include a
math.round...

..Net Framework 1.1 version 1.1.4322 SP1
Visual Basic - Visual Studio .Net 2003
Windows XP

Thanks,
 
J

Jon Skeet [C# MVP]

DJ said:
In an application that worked fine until recently I have started to have
problems with calculations involving two doubles. For example: -24.708 +
26.0 = 1.2920000000000016, however, -24.708 + 8.0 = -16.708. What would
cause this? I don't think the numbers are so large that it would be the
usual floating point calculation error because of rounding. How can I fix
it? I'd rather not change every calcuation in my program to include a
math.round...

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

Nick Malik [Microsoft]

DJ said:
Hello,

In an application that worked fine until recently I have started to have
problems with calculations involving two doubles. For example: -24.708 +
26.0 = 1.2920000000000016, however, -24.708 + 8.0 = -16.708. What would
cause this?

I don't see a problem here. Why do you?
I don't think the numbers are so large that it would be the
usual floating point calculation error because of rounding.

Who said size has anything at all to do with this calculation? Rounding
doesn't just happen for very large numbers. It happens for all of them.
How can I fix
it?

Decimal datatype
 

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