Type | Storage Requirement | Range(inclusive) |
---|---|---|
float | 4 bytes | Approximately 1.4E-45 to 3.4028235E38 ( 6-7 significant decimal digits) |
double | 8 bytes | Approximately 4.9E-324 to 1.7976931348623157E308 ( 15 significant decimal digits) |
The name double refers to the fact that these numbers have twice the precision of the float type (some people call these double-precision numbers). Here, the type to choose in most application is double. The limited precision of float is simply not sufficient for many situations. Seven significant (decimal) digits may be enough to precisely express your annual in dollars and cents, but it won't be enough for your company president's salary. It only makes sense to use float in the rare situations where the slightly faster processing of single-precision numbers is important, or when you need to store a large number of them.
Number of type float have a suffix F (for example: 3.14F). Floating-point numbers without an F suffix (such as: 3.14) are always considered to be of type double. You can optionally supply the D suffix (for example: 3.14D).
* Node: You can specify floating-point literals in hexadecimal. For example, 0,125 = 2 ^ -3 can be written 0x1.0p-3. In hexadecimal notation, you are p, not an e, to denote the exponent. (An e is a hexadecimal digit). Note that the mantissa is written in hexadecimal and the exponent in decimal. The base of the exponent is 2, not 10.
All floating-point computations follow the IEEE 754 specification. In particular, there are three special floating-point values to denote overflows and errors: Positive infinity, Negative infinity, NaN (not a number). For example, the result of dividing a positive number by 0 is positive infinity. Computing 0/0 or the square root of a negative number yields NaN.
* Node: The constants Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, and Double.NaN (as well as corresponding Float constants), but they are rarely used in practice. In particular, you cannot test:
if (x == Double.NaN) // is never true
if (Double.isNaN(x)) // check whether x is "not a number"
View Project
Download Project
0 comments:
Post a Comment