Reputation: 449
I am currently learning Java and in my book there's written a double can range from ~4,94E-324 to ~1,798E+308
I have some questions to that: How can a double then be zero (0)? Like double d = 0; Why is that possible when a double doesn't have a range including zero? And why are the negative numbers missing? Am I forgetting something here? Thanks for your help!
Sincerely, Maxi
Upvotes: 1
Views: 7780
Reputation: 3386
I think you're interpreting the range in a mathematical sense. What your book means by range is how small of a finite number and how large of a finite number Double
can produce, both for negative and positive values. So basically how close it can get to 0 with finite values and how close it can get to infinite with finite values. The actual range in a mathematical sense of Double
is something like -1.7*10^308
to 1.7*10^308
.
The Double
class has members which contain it's Min and Max value. Take a look at Min_VALUE
and Max_VALUE
members for Double
. Really the mathematical range is a byproduct of the range in your book ([-Double.MAX_VALUE, Double.MAX_VALUE])
, which is a result of how many degrees of accuracy Double
can hold.
Upvotes: 1
Reputation: 2177
Doubles and floats in most programming languages are more complicated than that. You can find a more technical explanation by looking up the IEEE 754 standard for floating point numbers.
Basically, floating point numbers (in java, the variable types float
and double
) are effectively stored in scientific notation, with a sign, mantissa, and exponent. (The base for the exponent is always 2). How this is converted into binary format is a bit complicated, but the important part to know is that the numbers are effectively stored as +/- mantissa * 2^exponent
.
The mantissa and exponent, however, both have fixed ranges. What your textbook is talking about is the range of values that are possible based on the range of the exponent. That is, how large or small can values be if you choose the largest or smallest possible exponent, ignoring the sign of the value. Zero is also ignored for this case, because in scientific notation, zero is a trivial case which does not illustrate the available range of exponents.
Doubles have about 15-16 digits worth of precision, that is, you can represent numbers with a mantissa of length 15-16 digits, regardless of the exponent. Regardless of the mantissa, you can represent numbers ranging from about 10^-324
to about 10^308
. And regardless of the mantissa and exponent, you can represent both positive and negative values.
Upvotes: 3