Java Floating point types

We’ll cover the following

  • Introduction
  • float
  • double
  • Scientific Notations

Introduction

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:

NameWidth in BitsApproximate Range
double644.9e–324 to 1.8e+308
float321.4e−045 to 3.4e+038

Floating-point numbers are numbers with a decimal point, such as 1.95 or 3.1415.

Java has two primitive types for floating-point numbers: float, which uses 4 bytes, and double, which uses 8 bytes.

In almost all cases, you should use the double type whenever you need numbers with fractional values.

The precision of a floating-point value indicates how many significant digits the value can have following its decimal point.

The precision of a float type is only about six or seven decimal digits, which isn’t sufficient for many types of calculations.

double variables have a precision of about 15 digits, which is enough for most purposes.

Floating-point numbers actually use exponential notation or scientific notation to store their values.

A floating-point number actually records two numbers: a base value (called the mantissa) and an exponent.

The actual value of the floating-point number is calculated by multiplying the mantissa by 2 raised to the power indicated by the exponent.

For float types, the exponent can range from -127 to +128. For double types, the exponent can range from -1,023 to +1,024. Thus both float and double variables are capable of representing very large and very small numbers.

When you use a floating-point literal, always include a decimal point, like this:

double period = 5.0;

That avoids the confusion of assigning what looks like an integer to a floating-point variable.

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:

package JAVA.Basic;

public class Area {
    public static void main(String[] args) {
        double pi, r, area;
        r = 10.8; // radius of circle
        pi = 3.1416; // pi, approximately
        area = pi * r * r; // compute area
        System.out.println("Area of circle is " + area);
    }
}

Scientific Notations for Floats And Doubles

We can use scientific notation when you write floating-point literals. The value of

double e = 5.10e+6;
is equivalent to
double e = 5100000D;
The sign is optional if the exponent is positive, so you can also write
double e = 5.10e6;
Note that the exponent can be negative to indicate values smaller than 1. The equation
double impulse = 23e-7;
is equivalent to
double impulse = 0.0000023;
We can add an F or D suffix to a floating-point literal to indicate whether the literal itself is of type float or double. For example:
float value1 = 123.45F; 

double value2 = 123456.1234D;
If you omit the suffix, D is assumed. As a result, you can usually omit the D suffix for double literals.

The floating-point numbers have two distinct zero values: a negative zero and a positive zero. Java treats them as equal.

That’s it!
You have successfully completed the post. Do Share : )

Peace Out!

Also Read – Java Integer types


Check Out Deals on -> Amazon , Flipkart , Myntra , Adidas , Apple TV , Boat , Canva , Beardo , Coursera , Cleartrip , Fiverr , MamaEarth , Swiggy


One comment

Leave a Reply

Your email address will not be published.