hi everyone, i am a hobie programmer and not an engineer.. actually i started learn c programing about a week ago, just for satisfy my curiosity... i was making many foolish mistakes with symbolic indicators thereby searched different c/c++ ides for debuging easily..
but now i have found little but significant difference pelles with eclipse and dev c/c++ ide's .. here is the program;
#include<stdio.h>
int main()
{
float i = 0.99;
printf("%3.20f\n", i);
}
pelles calculates 0.99000000953674316406 and other two calculates 0.99000000953674316000 ;
any idea? why there is different calculation by those.. and why all of them making those unnecessary fractions like 0.99000000953674316000 i mean 0.99000000000000000000 was expected...
https://en.wikipedia.org/wiki/Floating_point
QuoteSingle precision, usually used to represent the "float" type in the C language family (though this is not guaranteed). This is a binary format that occupies 32 bits (4 bytes) and its significand has a precision of 24 bits (about 7 decimal digits).
a float is 32-bits with a 24-bit mantissa, 23+1 implied bit or about 7 decimal digits
a double is 64-bit with a 53-bit mantissa, 52+1 implied bit or about 15.95 decimal digits
so change your printf to
printf("%3.7f\n", i);
To clarify a bit what others already mentioned. It's not that Pelle's C is "calculating" anything different than any other compiler using a 32bit float. It's that you request the compiler (by using a "%3.20f" format) to display such a 32bit float using 23 digits, while it has naturally only a 7 digit precision. that means anything after those 7 digits will be likely completely random. And within those 7 digits, both outputs you have given are identical.
And this is not a (Pelle's) C issue, it is an issue with any programming language, with (almost) all floating point numbers...
Ralf
thanks Ralf, i think that structure cant give exact mathmatical number point of 0.99000000000000000 but it can calculate (or compute) closest point... and that is, 0.99000000953674316406 ... i havent learned 4 math operation yet.. but certainly, float struckture not works if you planen to hit space asteroids with missiles.. thank you all..