Home | | **Object Oriented Programming** | | **The Complete Reference Java** | | **Internet Programming** | | **Web Programming** | Floating-Point Types

Floating-point numbers, also known as real numbers, are used when evaluating expressions that require fractional precision.

**Floating-Point Types**

Floating-point
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 floating-point type. Java
implements the standard (IEEE–754) set of floating-point types and operators.
There are two kinds of floating-point types, **float** and **double**, which
represent single- and double-precision numbers, respectively. Their width and
ranges are shown here:

Each of these floating-point types is examined next.

**float**

The type
**float** specifies a *single-precision* 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.

Here are
some example **float** variable
declarations:

float hightemp, lowtemp;

**double**

Double
precision, as denoted by the **double**
keyword, uses 64 bits to store a value. Double precision is actually faster
than single precision on some modern processors that have been optimized for
high-speed 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 large-valued 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; // radius of circle

pi =
3.1416; // pi, approximately

a = pi * r * r; // compute
area

System.out.println("Area
of circle is " + a);

}

}

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail

**Related Topics **

Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.