An easy, but one of the most often coded errors
-
I put it into this routine:
bool CheckSomething( const float x )
{
return (x == 5.0 ? true : false);
}Best, Jun
I think you would write
return (x == 5.0) ? true : false;
Right? But I'm interesting for the return value of your code. I riddle at the moment, if it returns true or false. Stephan\\\\\\|/// \\\\ - - // ( @ @ )
+---------------oOOo-(_)-oOOo-----------------+
| Stephan Pilz stephan.pilz@stephan-pilz.de |
| www.stephan-pilz.de |
| ICQ#: 127823481 |
+-----------------------Oooo------------------+
oooO ( )
( ) ) /
\ ( (_/
\_) -
I think you would write
return (x == 5.0) ? true : false;
Right? But I'm interesting for the return value of your code. I riddle at the moment, if it returns true or false. Stephan\\\\\\|/// \\\\ - - // ( @ @ )
+---------------oOOo-(_)-oOOo-----------------+
| Stephan Pilz stephan.pilz@stephan-pilz.de |
| www.stephan-pilz.de |
| ICQ#: 127823481 |
+-----------------------Oooo------------------+
oooO ( )
( ) ) /
\ ( (_/
\_)Stephan Pilz wrote:
Right?
wrong, You cannot compare float numbers like that, their internal representation will always ( more or less ) be different than what the real number is. X might be 5.0, but will more likelly be 4.99999999 or 5.00000001. so you will have to compare against a tolerance value.
if ( fabs( x-5.0 ) < 0.0000001 )
return true;
else
return false;or something like that.
Maximilien Lincourt Your Head A Splode - Strong Bad
-
Stephan Pilz wrote:
Right?
wrong, You cannot compare float numbers like that, their internal representation will always ( more or less ) be different than what the real number is. X might be 5.0, but will more likelly be 4.99999999 or 5.00000001. so you will have to compare against a tolerance value.
if ( fabs( x-5.0 ) < 0.0000001 )
return true;
else
return false;or something like that.
Maximilien Lincourt Your Head A Splode - Strong Bad
Maximilien wrote:
You cannot compare float numbers like that
No. I can compare. 5.0 and 5.000000 are alwasy the same representation in memory. If you would compare values from your example, you are right, but 5.0 is 5.000000 is 5.000 !! Ever. About the sense of the comparison can be argued of course. Stephan
\\\\\\|/// \\\\ - - // ( @ @ )
+---------------oOOo-(_)-oOOo-----------------+
| Stephan Pilz stephan.pilz@stephan-pilz.de |
| www.stephan-pilz.de |
| ICQ#: 127823481 |
+-----------------------Oooo------------------+
oooO ( )
( ) ) /
\ ( (_/
\_) -
I put it into this routine:
bool CheckSomething( const float x )
{
return (x == 5.0 ? true : false);
}Best, Jun
There should be a flag in compilers that stops you trying to do equality checks on floats.
cheers, Chris Maunder
CodeProject.com : C++ MVP
-
Maximilien wrote:
You cannot compare float numbers like that
No. I can compare. 5.0 and 5.000000 are alwasy the same representation in memory. If you would compare values from your example, you are right, but 5.0 is 5.000000 is 5.000 !! Ever. About the sense of the comparison can be argued of course. Stephan
\\\\\\|/// \\\\ - - // ( @ @ )
+---------------oOOo-(_)-oOOo-----------------+
| Stephan Pilz stephan.pilz@stephan-pilz.de |
| www.stephan-pilz.de |
| ICQ#: 127823481 |
+-----------------------Oooo------------------+
oooO ( )
( ) ) /
\ ( (_/
\_)Stephan Pilz wrote:
No. I can compare. 5.0 and 5.000000 are alwasy the same representation in memory. If you would compare values from your example, you are right, but 5.0 is 5.000000 is 5.000 !! Ever.
But he is not comparing 5.0 with 5.00000. He is comparing
x
with 5.0. X may be the result of (100.0 / 20.0) which may contain a rounding error. Therefore you have to build in a tolerance. Usually expressed asEpsilon
.
Upcoming Scottish Developers events: * UK Security Evangelists On Tour (2nd November, Edinburgh) * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog
-
I put it into this routine:
bool CheckSomething( const float x )
{
return (x == 5.0 ? true : false);
}Best, Jun
Sadly, I've seen this done way too many times even by "experienced" programmers.
-
There should be a flag in compilers that stops you trying to do equality checks on floats.
cheers, Chris Maunder
CodeProject.com : C++ MVP
-
There used to be a warning indicating that "equality comparisions between non-integers may not be meaningful" or something like that. It might have been in an old Fortran compiler though.
-
Stephan Pilz wrote:
Right?
wrong, You cannot compare float numbers like that, their internal representation will always ( more or less ) be different than what the real number is. X might be 5.0, but will more likelly be 4.99999999 or 5.00000001. so you will have to compare against a tolerance value.
if ( fabs( x-5.0 ) < 0.0000001 )
return true;
else
return false;or something like that.
Maximilien Lincourt Your Head A Splode - Strong Bad
And better yet... return ( fabs( x-5.0 ) < 0.0000001 );