Originally Posted by
flp1969
Here t is double, f is int and i is int (or int64_t, in my case)... But there is no "rouding" of a floating point value without this trick because there is no values representable between 0.299999999999999988897769753748434595763683319091 796875 and 0.300000000000000044408920985006261616945266723632 8125 in 'double' (you see? I can use 'bc' as well! hehe)...
I don't really understand the issue here
Code:
#include <stdio.h>
#include <math.h>
int main(void)
{
const double testvalues[] = {
3.14, 9.3, 9.28, 9.29, 1.1, 0.1, 0.9, 1.0/5, 1.0/3,
0.299999999999999988897769753748434595763683319091796875
};
double t;
int i, f;
unsigned it;
for (it = 0; it < sizeof testvalues / sizeof testvalues[0]; ++it) {
t = testvalues[it];
i = t;
f = round((t - i) * 100.0);
//f = ((t - i) + 0.005) * 100;
printf("%f: %d, %d\n", t, i, f);
}
return 0;
}
gives
Code:
3.140000: 3, 14
9.300000: 9, 30
9.280000: 9, 28
9.290000: 9, 29
1.100000: 1, 10
0.100000: 0, 10
0.900000: 0, 90
0.200000: 0, 20
0.333333: 0, 33
0.300000: 0, 30
Which is what is expected (?)