Real number types
A real number can have values that are
in between integers. The informal way to describe such number in English is as 'decimal'
numbers.
Examples of real numbers include: 0.0,
34.6, 99.2, -0.002 etc.
Java offers two ways to represent and work
with different types of real number. The difference between them are both the range and
precision of values they can represent. By precision, it is mean the number of decimal
places that numbers can be stored to.
The two floating point number types are:
Real numbers are only approximated by float and double, because they have limited precision. For example, there is no way to
represent the result of '1 divided by 3' exactly as a decimal number, as it requires
indefinite precision (0.333333333….). The double type gives about 17 digits precision,
which is enough for most purposes.
Since most mathematical operations are now
done by the computer's hardware, there is little to be gained by using the float data type rather than double.
In fact, if you don't indicate otherwise, the
Java compiler will assume that you want double and not float precision. For example if
you write:
2.1
+ 3.4
Java will evaluate this expression to a value
of type double.
Unless you are writing quite sophisticated
mathematical programs, you probably don't need to worry too much about these issues.
Back
to top
. 
RITSEC - Global Campus
Copyright ?1999 RITSEC- Middlesex University. All rights reserved.
webmaster@globalcampus.com.eg
|