float comparation problem

A

aBiNg

Here is the code:

#include <stdio.h>
#define float_cmp(x, y) ((int)((x) * 100 + 0.5) / 100. >= (int)((y) *
100 + 0.5) / 100.) ? 1 : 0

int main(void)
{
float a = 0.014, b = 0.015;

printf("The result is: %d\n", float_cmp(a, b));
return 0;
}

I compiled this source with gcc-3.4.5, and run with it, the output is:
The result is: 1

(b) * 100 + 0.5 is equal to 2.000000, then i think (int)((b) * 100 +
0.5) should be 2, but this is not true. In my system, it is 1.
I don't know the type-cast mechanism of gcc, can anybody show me the
reason?

Any help will be very appreciated.
 
A

aBiNg

Your explainations make this code reasonable. Thanks a lot.

I am pondering how to add a patch to make this code work. :)
 
L

luserXtrog

Your explainations make this code reasonable. Thanks a lot.

I am pondering how to add a patch to make this code work. :)

#include <math.h>
double round(double x);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top