floating point
-
void main { float a = 5.0 , b = 0.1 ; float c ; c = a + b ; if( c >= 5.1 ) { printf( "OK" ) ; } else { printf( "Not OK" ) ; } //in the above code c does not contain the the result i expected , it filled with 5.099999 and i get the output Not OK. how i can check the value of c ? please help me
Thanks & Regards
-
void main { float a = 5.0 , b = 0.1 ; float c ; c = a + b ; if( c >= 5.1 ) { printf( "OK" ) ; } else { printf( "Not OK" ) ; } //in the above code c does not contain the the result i expected , it filled with 5.099999 and i get the output Not OK. how i can check the value of c ? please help me
Thanks & Regards
That's because of floating point precision. Your float will never be represented exactly. Check this[^]
Cédric Moonen Software developer
Charting control [v1.4] -
void main { float a = 5.0 , b = 0.1 ; float c ; c = a + b ; if( c >= 5.1 ) { printf( "OK" ) ; } else { printf( "Not OK" ) ; } //in the above code c does not contain the the result i expected , it filled with 5.099999 and i get the output Not OK. how i can check the value of c ? please help me
Thanks & Regards
nitin3 wrote:
//in the above code c does not contain the the result i expected , it filled with 5.099999 and i get the output Not OK. how i can check the value of c ? please help me
Try this... works for me...
void main()
{
float a = 5.0f , b = 0.1f ;
float c = a + b;if( c >= 5.1f )
{
printf( "OK" ) ;
}
else
{
printf( "Not OK" ) ;
}
}Always append "f" to a float value else it's treated as a double, earlier you were comparing a float var and a double var. Explained further -> http://nibuthomas.wordpress.com/2007/06/08/comparing-two-float-values/[^]
Nibu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com
-
void main { float a = 5.0 , b = 0.1 ; float c ; c = a + b ; if( c >= 5.1 ) { printf( "OK" ) ; } else { printf( "Not OK" ) ; } //in the above code c does not contain the the result i expected , it filled with 5.099999 and i get the output Not OK. how i can check the value of c ? please help me
Thanks & Regards
floating point numbers are limited in resolution. They can get very close to the result, but not necessarily exact. You can minimize the problem by using "double" instead of "float", but that doesn't eliminate things. One quick fix is to simply use a small delta value in your computations. For example: float delta = 0.000000001; where delta is some number much smaller than you care about in your program. Then for greater than comparisons include it if (c >= (5.1 + delta)) printf("OK");
-
floating point numbers are limited in resolution. They can get very close to the result, but not necessarily exact. You can minimize the problem by using "double" instead of "float", but that doesn't eliminate things. One quick fix is to simply use a small delta value in your computations. For example: float delta = 0.000000001; where delta is some number much smaller than you care about in your program. Then for greater than comparisons include it if (c >= (5.1 + delta)) printf("OK");
Kwanalouie wrote:
float delta = 0.000000001;
Correctly defined as epsilon.
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
void main { float a = 5.0 , b = 0.1 ; float c ; c = a + b ; if( c >= 5.1 ) { printf( "OK" ) ; } else { printf( "Not OK" ) ; } //in the above code c does not contain the the result i expected , it filled with 5.099999 and i get the output Not OK. how i can check the value of c ? please help me
Thanks & Regards
Another approach: Use rational numbers. Use ints for the numerator and denominator. It requires a bit more coding, but your fractional numbers will be exact.