Java

# Java Float and Double Comparison

## Java Float and Double Comparison

The float and the double types are two different formats for representing real numbers in Java. They are both referred to as the floating-point types. A real number in math is a whole number and a decimal part. For example, 23.75 is a real number. The whole number part is 23 and the decimal part is 75. If the decimal part is “.0”, the real number is 23.0, then the real number is an integer. Integers are a sub-set of real numbers. A range of real numbers from the smallest to the biggest value would have integers within. For example, the range of real numbers from -3.5 to +3.5 has the integers, -3, -2, -1, 0, +1, +2, and +3, within. Do not forget that the decimal part of a real number is a proper fraction. For example .75 is 3/4.

As seen from the above range, which is from -3.5 to +3.5, a number can be positive or negative. A number without the sign is called the absolute number. The absolute number of -2.5 is 2.5. The absolute number of +2.5 is 2.5. The absolute number is the positive number.

The number, 23.75 can be represented by a float or a double. So, how does the difference come about? The answer to this can be appreciated by trying to answer the question: How many numbers are there between 2 and 3, inclusive (including 2 and 3)? In fact, the number of numbers between 2 and 3 are infinite. So, float or double numbers are a set of numbers in a range, as infinite numbers cannot be determined. For the same range, there are more double numbers, with shorter intervals between each consecutive pair of numbers.

This article compares floats and doubles in Java, beginning with an imaginary corresponding types called flot and doub.

## Imaginary Corresponding Types

Let us develop our own imaginary corresponding types called flot and doub, corresponding to float and double.

## Flot

With the flot type, let us have three numbers between 2 and 3. Let us have the numbers, 2.25, 2.5, and 2.75. So, the only flot numbers between 2 and 3 inclusive, are 2, 2.25, 2.5, 2.75 and 3. As can be seen from this range, the smallest absolute number in the entire flot set is 0.25, which is also the difference between two consecutive numbers. (In order to have the numbers from 3 to 4, just continue to add 0.25).

## Doub

With the doub type, let us have seven numbers between 2 and 3. Let us have the numbers, 2.125, 2.25, 2.375, 2.5, 2.625, 2.75, 2.875. So, the only doub numbers between 2 and 3 inclusive, are 2, 2.125, 2.25, 2.375, 2.5, 2.625, 2.75, 2.875 and 3. As can be seen from this range, the smallest absolute number in the entire doub set, is 0.125, which is also the difference between two consecutive numbers. (In order to have the numbers from 3 to 4, just continue to add 0.125). This number is smaller than 0.25 for flot.

## Margin of Error

Note that consecutive numbers for doub are smaller in interval than consecutive numbers for flot.

The exact number of numbers within a range of flots or doubs cannot be determined, as all the numbers in the range are infinite. So, there is a margin of error for any number given, including integers, such as 2 or 3, for any of these types.

For flots, for any number given, the number is accurate to within, + or – 1/4 (i.e. 0.25). So, a flot number of 2.5, actually lies somewhere between 2.375 and 2.675. A flot number of 3, actually lies somewhere between 2.875 and 3.125. The actual number (precision) can never be determined. Even an integer of a flot type is a number that is not very certain.

For doubs, for any number given, the number is accurate to within + or -1/8 (0.125). So, a doub number of 2.5, actually lies somewhere between 2.4375 and 2.5625. A doub number of 3, actually lies somewhere between 2.9375 and 3.0625. The actual number (precision) can never be determined. Even an integer of a doub type is a number that is not very certain. Notice that a doub number has more precision than a flot number.

The float and double types in java are correspondingly similar to these imaginary flot and doub types. Their intervals are correspondingly smaller.

## Comparison of Float and Double Proper in Java

Float is known as a single precision type. Double is known as a double precision type. A float number occupies 32 bits (four bytes). A double number occupies 64 bits (eight bytes). Both numbers represent real numbers in Java, with the double type having less intervals between consecutive numbers. Both types can never give an exact number. However, the double has less tolerance than the float. Both types are actually based on formats of IEEE 754.

The range of the double type from its negative end to its positive end, is wider than the range of the float type, from its negative end to its positive end.

## Float

The smallest normalized positive number of the float type is 2–126. The largest positive number for the float is (2 – 2–23) × 2127. The set of float numbers can be imagine as adding 2–126 repeatedly from the negative end to its positive end.

A float number value should end with ‘f’, as the following program shows:

public class TheClass {
public static void main(String[] args) {
float flt = 23.75f;
System.out.println(flt);
}
}

The output is:

23.75

## Double

The smallest normalized positive number of the double type is 2–1022, smaller than that of float. The largest positive number for the double is (2 – 2–52) × 21023. The set of double numbers can be imagine as adding 2–1022 repeatedly from the negative end, to its positive end.

A double number value does not end with ‘f’ or ‘d’ or ‘r’, as the following program shows:

public class TheClass {
public static void main(String[] args) {
double dbl = 23.75;
System.out.println(dbl);
}
}

The output is:

23.75

but with less margin of tolerance, than with the float above.

Float and double are primitive types. And so, no class has to be imported into the program by the programmer for them to be used.

## NaN

NaN stands for Not-a-Number. Consider the following program:

public class TheClass {
public static void main(String[] args) {
System.out.println(0/0);
}
}

The program compiles without any problem, but issues the following error message, when run:

Exception in thread “main” java.lang.ArithmeticException: / by zero
at TheClass.main(TheClass.java:3)

The invalid operation here is, zero-divided-by-zero. The result is NaN, though an error message was issued. This is applicable to both float and double. Other invalid operations, result in NaN.

## Conclusion

Float is known as a single precision type. Double is known as a double precision type. A float number occupies 32 bits (four bytes). A double number occupies 64 bits (eight bytes). Both types represent real numbers in Java, with the double type having less intervals between consecutive numbers, and is more precise.

Chrys