double

The type double refers to floating-point numbers stored in 64 bits, as described in the IEEE[1] standard reference 754. The type double will be the default type you use when you want to do some calculations that might involve decimal places (i.e., not integral values).

range of values: These provide numbers that can range between about –1.7E308 to +1.7E308 with about 15 significant figures of accuracy. The exact accuracy depends on the number being represented. Double precision floating-point numbers have the range shown in Figure 4-1.

Figure 4-1. Type double should be used when calculations involve decimal places

image

IEEE 754 arithmetic has come into virtually universal acceptance over the last decade, and it would certainly raise a few eyebrows if a computer manufacturer proposed an incompatible system of floating-point numbers now. IEEE 754 is the standard for floating-point arithmetic, but there are several places where chip designers can choose from different alternatives within the standard, such as rounding modes and extended precision. Java originally insisted on consistency on all hardware by specifying the alternatives that must be used. That is now loosened somewhat with strictfp.

IEEE 754 has an ingenious way of dealing with the problem of representing, on limited hardware, the unlimited amount of infinite precision real-world numbers. The problem is resolved by reserving a special value that says, “Help! I've fallen off the end of what's representable and I can't get up.” You're probably familiar with infinity, but the “Not-a-Number” might be new if you haven't done much numerical programming. Not-a-Number, or NaN, is a value that a floating point can take to indicate that the result of some operation is not mathematically well-defined, like dividing zero by zero.

If you get a NaN as an expression being evaluated, it will contaminate the whole expression, producing an overall result of NaN—which is exactly what you want! The worst way to handle a numeric error is to ignore it and pretend it didn't happen.

You may never see a NaN if your algorithms are numerically stable, and you never push the limits of your datasets. Still, it's nice to know that NaN is there, ready to tell you your results are garbage, if they head that way.

literals: It is easiest to show by example the valid formats for double literals:

1e1   2.   .3    3.14    6.02e+23d

The format is very easy-going; just give the compiler a decimal point or an exponent, and it will recognize that a floating-point literal is intended. A suffix of “D”, “d”, or no suffix, means a double literal. In practice, most people omit the suffix.

It is also permissible to assign any of the integer literals or character literals to floats or doubles. The assignment doesn't need a cast, as you are going from a less capacious type to a more capacious type.

So a line like this, while perverse, is valid:

double newline = ' ';

It takes the integer value of the literal (0x0a in this case), floats it to get 10.0d, and assigns that to “newline.” Don't ever do this.


[1] IEEE is the Institute of Electrical and Electronic Engineers, a U.S. professional body.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.81.33