It's better to put the code directly in your post if it's short. The problem is that floating-point values aren't always exact, so you can't reliably test for equality. The usual fix for your problem is to do a "close enough" test using the difference of the two values and a suitably small epsilon value:
#include <stdio.h>
#include <math.h>
static double floating_epsilon ( int precision )
{
double result = 0.1;
while ( --precision >= 0 )
result /= 10;
return result;
}
int main ( void )
{
float a = 1.1;
double b = 1.1;
if ( fabs ( a - b ) <= floating_epsilon ( 1 ) )
printf("Both are equal\n");
else
printf("Both of them are not equal\n");
return 0;
}
Reputation Points: 44
Solved Threads: 8
Junior Poster in Training
Offline 62 posts
since Oct 2007