This example is from this forum. Posted by Salem.
Solution is obvious, but doesn't work. Can somebody (Salem) take a look at this.
Here is the result:
Input:234.456787
step 1:23445.679688
step 2:23446.179688
step 3:23446.000000
step 4:234.460007
Old=234.456787, new=234.460007
I tried several numbers and I never gor correct number.
For some numbers I would get 234.4699998 ... etc...
Something is wrong with division part.
Thanks
Code:
#include <stdio.h>
#include <math.h>
typedef float type;
type RoundVal(type num)
{
printf("Input:%f\n", num);
num *= 100;
printf("step 1:%f\n", num);
num += 0.5;
printf("step 2:%f\n", num);
num = floor( num );
printf("step 3:%f\n", num);
num /= 100.0;
printf("step 4:%f\n", num);
return num;
}
int main () {
type a = 234.456789;
printf( "Old=%f, new=%f\n", a, RoundVal(a) );
return 0;
}
Solution is obvious, but doesn't work. Can somebody (Salem) take a look at this.
Here is the result:
Input:234.456787
step 1:23445.679688
step 2:23446.179688
step 3:23446.000000
step 4:234.460007
Old=234.456787, new=234.460007
I tried several numbers and I never gor correct number.
For some numbers I would get 234.4699998 ... etc...
Something is wrong with division part.
Thanks