I have a program where I am calculating large distances. I then convert the distance (a double) using stringfromnumber to get the commas inserted (i.e. 1,234,567). This works fine as long as the distance is below 2,000,000,000. Any time I exceed 2 billion I get a negative number that is also not the correct distance. From checking the help, I seem to be well within the range of a double.
Asked
Active
Viewed 1.0k times
2 Answers
8
You can check you're own float.h
for DBL_MAX
to find out. I got a value of 1.797693e+308 from:
#include <stdio.h>
#include <float.h>
int main ( void ) {
printf("Max double %e \n", DBL_MAX);
return 0;
}

thelaws
- 7,991
- 6
- 35
- 54
5
A double can hold anything from infinity to negative infinity. However It can only hold 15 digits accurately. Also, keep in mind that if you cast it to an int and the double stores something larger than 2,147,483,648, the int will overflow to a negative number.

Skyler Saleh
- 3,961
- 1
- 22
- 35
-
7From infinity to negative infinity isn't a very good description, since that is physically impossible by the definition of infinity. The maximum exponent for a double is 1023 and the minimum is −1022, so it can represent the range from 2^-1022 to 2^1023 with up to 15 digits, as well as ±infinity and NaN. – ughoavgfhw May 01 '11 at 22:25
-
1That is true, but most people cant grasp how large that range of numbers is (2^1023 atoms of a gas will overfill a container that is a googol liters), and in technicality the largest number a double can hold is infinity and the smallest number a double can hold is -infinity. – Skyler Saleh May 01 '11 at 22:37
-
Thanks for the quick reply. That is exactly what is happening. Any way to make a number larger than 2,147,483,648 display correctly? – tombuarts May 01 '11 at 23:02
-
1If you must cast it, try going to a long. Unless you're calculating distances to the edge of the known universe in nanometers—and even then you should be all set with a long (or long long). – FeifanZ May 01 '11 at 23:07
-
Otherwise, is the issue before or after the commas? – FeifanZ May 01 '11 at 23:08
-
1If you are using a NSNumberFormater... try [numberFormater stringFromNumber:[NSNumber numberWithDouble:doubleVar]]; where numberFormater is your formatter and doubleVar is your variable – Skyler Saleh May 01 '11 at 23:08