Page 471 - Introduction to Programming with Java: A Problem Solving Approach
P. 471
11.2 Integer Types and Floating-Point Types 437
digits are merely approximations. This round-off error is compounded when you have repetitive calcula- tions. Since memory is now relatively inexpensive, you should consider float to be an archaic data type, and you should usually avoid it. An exception is when you specify color. Several methods in the Java API Color class employ float type parameters and/or return values.
Be aware that floating-point numbers do worse than integer numbers when it comes to precision. For example, when comparing the 32-bit float type and the 32-bit int type, the floating-point type has less precision. float numbers have 6 digits of precision, whereas int numbers have 9 digits of precision. Likewise, when comparing the 64-bit double type and the 64-bit long type, the floating-point type has less precision. double numbers have 15 digits of precision, whereas long numbers have 19 digits of preci- sion. Why do floating-point numbers lose out on precision? Some of the bits in floating-point numbers are used to specify the exponent that allows these numbers to take on much greater ranges in magnitude than integer numbers can take on. This reduces the bits available to supply precision.
As you learned in Chapter 5, Float and Double are the wrapper classes for the float and double data types. To access a floating-point data type’s minimum and maximum values, use the Float and Double classes’ MIN_NORMAL and MAX_VALUE named constants. MAX_VALUE is a floating-point data type’s largest positive value, and MIN_NORMAL is a floating-point data type’s smallest full-precision positive value. A floating-point’s MIN_NORMAL is qualitatively different from an integer’s MIN_VALUE. Instead of being a large negative value, a floating-point MIN_NORMAL is a tiny positive fraction. So what are the limits of negative floating-point numbers? The largest-magnitude negative number a floating-point variable can hold is -MAX_VALUE. The smallest-magnitude negative number a floating-point variable can hold safely is -MIN_NORMAL, a tiny negative fraction.
Actually, it’s possible for a floating-point variable to hold a number whose magnitude is smaller than MIN_NORMAL. It can hoAld pa avalgueoas sPmaDll Fas a Eflonatihnga-ponintcMeINr_VALUE, which is approximately 1.4 * 1045 for float and approximately 4.9 * 10324 for double. But the MIN_VALUE of a floating-point number has only one bit of precision, and that could produce a significant error in a computed result—with- out any explicit indication that an error is present. This is an example of the worst kind of bug, because it can go unrecognized for a long time. Therefore, with floating-point numbers, always use MIN_NORMAL instead of MIN_VALUE.
The default floating-point constant type is double. If you declare a variable to be a float, you must append an f or F suffix to all floating-point constants that go into it, like this:
float gpa1 = 3.22f;
float gpa2 = 2.75F;
float gpa3 = 4.0;
compilationerror,because4.0isadouble
Because of the f and F suffixes, 3.22f and 2.75F are 32-bit float values, so it’s legal to assign them into the 32-bit gpa1 and gpa2 float variables. But 4.0 is a 64-bit double value, and attempting to assign it into the 32-bit gpa3 float variable generates a compilation error.
To write a floating-point number in scientific notation, put e or E before the base-10 exponent value. If the exponent is negative, insert a minus sign between the e or E and the exponent value. If the exponent is positive, you may use a plus sign after the e or E, but it’s not standard practice. In any event, there must never be any whitespace within the number specification. For example:
double x = -3.4e4;
double y = 5.6E-4;
equivalent to 34000.0 equivalent to 0.00056