tombuarts
tombuarts

Reputation: 115

What is the range of a double in Objective-C?

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.

Upvotes: 3

Views: 10080

Answers (2)

thelaws
thelaws

Reputation: 8001

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;
}

Upvotes: 8

Skyler Saleh
Skyler Saleh

Reputation: 3991

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.

Upvotes: 5

Related Questions