+84 1649 660 740

Sunday, 17 August 2014

Floating Point Types In Java

The floating-point types denote numbers with fractional parts. The two floating-point types:

TypeStorage RequirementRange(inclusive)
float4 bytesApproximately 1.4E-45 to 3.4028235E38 ( 6-7 significant decimal digits)
double8 bytesApproximately 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
to check whether a particular result equals Double.NaN. All "not a number" values are considered distinct. However, you can use the Double.isNaN method:

if (Double.isNaN(x)) // check whether x is "not a number"
* Caution: Floating-point numbers are not suitable for financial calculations in which roundoff errors cannot be tolerated. For example, the command System.out.println (2.0 - 1.1) prints 0.89999999,.. not 0.9 as you would expect. Such roundoff errors are caused by the fact that floating-point numbers are represented in the binary number system. There is no precise binary representation of the faction 1/10, just as there is no accurate representation of the fraction 1/3 in the decimal system. If you need precise numerical computations without roundoff errors, use the BigDecimal class, which is introduced later.

View Project

Download Project

0 comments:

Post a Comment