floating point comparision
-
main() { int x=2.4; if(x==2.4) cout<<"true"; else cout<<"false"; return 0; }
Well it is not working, help me out please!Spread wat u Know!
You cannot compare FP numbers in this fashion. You'll need something like:
if (fabs(x - 2.4) < 0.00001)
cout << "true" << endl;See here for more.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
You cannot compare FP numbers in this fashion. You'll need something like:
if (fabs(x - 2.4) < 0.00001)
cout << "true" << endl;See here for more.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
Thnx but I did visit Bruce Dawson But couldnt get it in nutshell Please will u?
Spread wat u Know!
in short, a computer cannot "always" represent a floating point number accurately in binary format, and a precision error will be inserted when converting back and forth between the floating point number and the binary representation. for example : 2.4 can be "errored" to something like either 2.39999999 or 2.40000001
Maximilien Lincourt Your Head A Splode - Strong Bad
-
main() { int x=2.4; if(x==2.4) cout<<"true"; else cout<<"false"; return 0; }
Well it is not working, help me out please!Spread wat u Know!
Cmania wrote:
int x=2.4;
you need to go look at the definition of "int"
image processing toolkits | batch image processing | blogging
-
in short, a computer cannot "always" represent a floating point number accurately in binary format, and a precision error will be inserted when converting back and forth between the floating point number and the binary representation. for example : 2.4 can be "errored" to something like either 2.39999999 or 2.40000001
Maximilien Lincourt Your Head A Splode - Strong Bad
-
main() { int x=2.4; if(x==2.4) cout<<"true"; else cout<<"false"; return 0; }
Well it is not working, help me out please!Spread wat u Know!
Here's an CodeProject article that discusses this issue: http://www.codeproject.com/tips/FloatingPointEquality.asp[^]
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001