0

I don't understand why when I add 1 to a big float number I can't see a difference. I suppose it's linked to the precision ? But I cannot figure out how to solve it. Can someone explains me why this happen and how I can see if "1" was correctly added to the variable "hello" ?

#include <iostream>
#include <limits>

using namespace std;

int main()
{
   float hello = 2227169280.0f;
   
   cout<<"Hello: " << hello << endl;
   printf("%f\n",hello);
   hello = hello + 1.0f;
   cout <<"Hello: " << hello << endl;
   printf("%f\n",hello);
   return 0;
}

Result:

Hello: 2.22717e+09                                                                                                                            
2227169280.000000                                                                                                                             
Hello: 2227169280                                                                                                                             
2227169280.000000 //<-- why it's not 2227169281.000000 ??

Thank you a lot.

  • 2
    Does this answer your question? [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Brian61354270 Apr 12 '21 at 23:51
  • Yes thank you @Brian, in the end I will avoid float, as I can resume , more the float is big, less the precision is "precise". Then I should replace it by using an "unsigned long int" for my case because I do not care about the floating part. (I just needed to handle big numbers) – anotherlostedguy0 Apr 13 '21 at 00:06
  • @anotherlostedguy0 — the precision is the same throughout the range of representable values. – Pete Becker Apr 13 '21 at 02:49

0 Answers0