NO

Author Topic: hello world  (Read 3490 times)

ibra

  • Guest
hello world
« on: April 09, 2016, 01:00:41 PM »
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;

Code: [Select]
#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...
« Last Edit: April 10, 2016, 02:49:32 PM by frankie »

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: hello world
« Reply #1 on: April 09, 2016, 01:38:12 PM »
https://en.wikipedia.org/wiki/Floating_point
Quote
Single 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).
May the source be with you

Offline jack

  • Member
  • *
  • Posts: 66
Re: hello world
« Reply #2 on: April 10, 2016, 12:22:24 AM »
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
Code: [Select]
printf("%3.7f\n", i);

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: hello world
« Reply #3 on: April 10, 2016, 07:59:06 AM »
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

ibra

  • Guest
Re: hello world
« Reply #4 on: April 10, 2016, 09:20:48 AM »
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..
« Last Edit: April 10, 2016, 09:47:20 AM by ibra »