FloatingPoint Types in java
By: aathishankaran Emailed: 1765 times Printed: 2513 times
Latest comments

FloatingPoint Types in java
Floatingpoint numbers, also known as real numbers, are used when evaluating expressions that require fractional precision. For example, calculations such as square root, or transcendentals such as sine and cosine. Result in a value whose precision requires a floatingpoint type. Java implements the standard (IEEE754) set of floatingpoint types and operators. There are two kinds of floatingpoint types, float and double, which represent single and doubleprecision numbers, respectively. Their width and ranges are shown here:
Name width in Bits Range
double 64 1.7e308 to 1.7e+308
float 32 3.4e038 to 3.4e+038
float
The type float specifies a singleprecision value that uses 32 bits of storage. Single precision is faster on some processors and takes half as much space as double precision, but will become imprecise when the values are either very large or very small. Variables of type float are useful when you need a fractional component, but don’t require a large degree of precision. For example, float can be useful when representing dollars and cents.
Double
Double precision, as denoted by the double keyword, users 64 bits to store a value. Double precision is actually faster than single precision on some modern processors that have been optimized for highspeed mathematical calculations. All transcendental math functions, such as sin(), cos(), and sqrt(), return double values. When you need to maintain accuracy over many iterative calculations, or are manipulating largevalued numbers, double is the best choice.
Here is a short program that uses double variables to compute the area of a circle:
//compute the area of a circle.
class Area {
Public static void main (String args[]) {
double pi, r, a;
r = 10.8;
pi = 3.1416;
a= pi * r * r;
System.out.println(“Area of circle is “ + a”);
}
}
Java Home  All Java Tutorials  Latest Java Tutorials
Sponsored Links 
If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.

Comments(0)
Be the first one to add a comment
Most Viewed Articles (in Java )

Most Emailed Articles (in Java)
