Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

2.00 ISNT == to 2.00 and I've no idea why.... 1

Status
Not open for further replies.

Sedai

Programmer
Mar 4, 2001
158
US
i have double variable that is assigned the value of a function.
I cerr the variable with 20 precision and it gives
2.000000000000... then I compare it with another variable that is equal to 2.00 and the if statement x==y doesnt execute. insted the else statement executes.
just to test, in the else statemnt I put cerr << x << &quot; &quot;<< y;
and it gives 2.00000 2.00000

what could be the problem.
it executes the statment if x<2.00

please help. that's truly STRANGE.
probably it's something simple but I don't get it.
thank you. Avendeval


 
comparing double-type variables for equality is highly
doubtful. this works correctly with ints or chars.
if I have to compare that way, I usually define a variable
ZERO which is 0.000001. then I calculate the difference
between variable a and b and with abs() I make sure the
result is positive. that result I will compare with ZERO for LESS.
try it out that way.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top