Wednesday, July 24, 2013

Floating-Point Data Types

Numeric values that are not integral are stored as floating-point numbers. A floating-point number has a fixed number of digits of accuracy but with a very wide range of values. You get a wide range of values,even though the number of digits is fixed, because the decimal point can “float.” For example, the values 0.000005, 500.0, and 5000000000000.0 can be written as 5×10-6, 5×102, and 5×1012 respectively - you have just one digit 5 but you get three different numbers by moving the decimal point around.

There are two primitive floating-point types in Java, type float and type double. These give you a choice in the number of digits precision available to represent your data values, and in the range of values that can be accommodated:

float : -3.4E38 (-3.4 * 1038) to +3.4E38 (+3.4 * 1038) and occupy 4 bytes
double : -1.7E308 (-1.7 * 10308) to +1.7E308 (+1.7 * 10308) and occupy 8 bytes

As with integer calculations, floating-point calculations in Java will produce the same results on any computer.

No comments:

Post a Comment

Thanks for Commenting......