I have recently come across something weird that I am having trouble understanding.
Basically I was trying to estimate the number of seconds that have passed in the last 2014 years. However the results that I was getting were negative numbers. I thought at first that it was some kind of overflow due to the size limitations of a double floating point variable, however after looking into that it seems that this is not the case. So I started to play around with the following code:
double test = 1.8*pow(10,308);
cout << test << endl;
test = 1.7*pow(10,308);
cout << test << endl;
test = 2*1000000000;
cout << test << endl;
test = 2*100*100*100*100*100;
cout << test << endl;
test = 1*1000000000;
cout << test << endl;
test = 1*100*100*100*100*100;
cout << test << endl;
This returned the following output:
inf
1.7e+308
2e+009
-1.47484e+009
1e+009
1.41007e+009
The first two results make sense to me. I am under the impression that 1.7e+308 is the maximum possible value that can be stores in a double, which means that 1.8e+308 wouldn't work - so the compiler interprets this as infinity?
However after that things become a little confusing.
I don't understand why 2*10000000000 and 2*100*100*100*100*100 return different values?
Or why 1*10000000000 and 1*100*100*100*100*100 don't return that same value?
I also don't understand why 2*100*100*100*100*100 returns a negative value?
Also where is the 1.4... coming from when I am only multiplaying by 1 or 2?
If someone could help me understand this better it would be appreciated!