Comparing a double to -9.25596e+061

Hi, I would like to know how I could compare the number initialized as double result to the actual number -9.25596e+061.

Since it is the only comparison possibility I know, I naturally tried:
if (result!=-9.25596e+061)
which compiles fine but doesn't actually work (whether result is this number or not, the if loop is ignored...)

Could you help me with that?
Thank you

Sky
"If" isn't a loop, and you need to put somehing in there. Try this:cout<<boolalpha<<(result==-9.25596e+061);
The problem may be that your double may be rounding. As a result, though the two numbers should be exactly equal, when performing the operation the computer may round slightly making the two numbers not strictly equal. You could try something like this.
1
2
3
4
5
6
double result=<some number>;
double epsilon=<define your limit here>;
double test=-9.25596e+061;
if (abs(test-result)<epsilon) {
...
}


To use this you would need to know what a reasonable number for epsilon would be, and that would be determined by the kind of problem you are trying to solve. This is just a simple solution, there are better solutions out there but they are more complicated.
The value -9.25596e+061 is not representable as a double. It lies between two valid doubles:

-92559600000000004169081825175203882875579379644394960037347328 (that's -8106476583657459 * 2153)

and

-92559599999999992751100283527524834409291624048433868975374336 (that's -8106476583657458 * 2153)

Since the first number is a bit closer to -9.25596e+061, it is what's stored when you attempt to write -9.25596e+061 to a double, and that's what you need to be comparing with, if you're using exact comparison.

The usual approach is to use an appropriately scaled epsilon, as already mentioned.
Last edited on
Indeed, I'm going to try that asap.
Thank you all very much

Sky
Topic archived. No new replies allowed.