wrong result.

In my program i have the following: 84.74 - 84 - 0.74 and it says it equals to -2.14577e-006 , how do i fix this? shouldn't it be 0?

i have:
int = 84 ,
double = 84.74 ,
float = 0.74
Last edited on
post the code in brackets please...

Let me take a look at it...
Floating point representations are not exact, and -2.14577×10-6 is basically 0.
https://en.wikipedia.org/wiki/Floating_point#Accuracy_problems
i'd rather not post it all, i will have to change alot of it, but what i posted above is all included in my problem.
Last edited on
[quote = Zhuge]Floating point representations are not eact[/quote]

You beat me to it .-.
what do you suggest i do?
Last edited on
Instead of expecting exact results from floating point math, handle things based on a range. For example, instead of checking to see if that your computation returned exactly 0, check if it is, say, in the range of [-0.01, 0.01] (or whatever range is appropriate for your work).
and another stupid question, how would you suggest i do this, with switch?
Last edited on
Well first off, why are you intermixing doubles and floats? It might not still be exactly zero, but I'm pretty sure it would be at least closer if you did a double minus a double instead of a double minus a float.

Anyway, you'd want to do something like this

1
2
3
4
5
const double Threshold = 0.0001;
if ( abs( my_value - what_my_value_should_be ) < Threshold)
{
    cout << "It's close enough to zero.\n";
}
Last edited on
Topic archived. No new replies allowed.