Introduction
In the previous blog, we spoke about the data types used to represent whole numbers; today, we look at those that are used to represent numbers with fractional parts. These are floating-point types.
In Java, there are 2 floating-point types, these are float and double. The float has a memory allocation of 32 bits, whilst the double has a memory allocation of 64 bits. By default, all whole numbers with a fractional side are stored as double since it allows for more accuracy and fewer rounding errors.
Difference Between Float and Double
Aside from the fact that they both occupy very different sizes in memory (with double having twice as much space as floats) The double allows for more precision when it comes to decimal points.
For example, if you had a decimal you wanted to store and this decimal number had up to 10 decimal places, if you store it in a float, you will only have access to about 6 or 7 of its decimal places. This would mean that the stored value will have a higher error margin as opposed to the case where the decimal is stored in a double, which allows for more decimal places
Note: It is good to know that in Java float numbers end with an f or F. However, a decimal number without any letter attached to it will be taken as double by the compiler.
To represent a double number, one can simply write the decimal number as it is or decide to end it with d or D indicating double.
Certain Important Information You Should Know
To simulate scientific notation the letter e or E is used to show exponent. For example if you are trying to write 1.729 * 10^3 in code you will simply type double large = 1.729E3; // 1.729 × 10^3 = 1729
But in the case where you are using the hexadecimal system to write these numbers E or e will have significant impact since E corresponds to the number 14 in that system. Hence we could represent it using the letter p this way: double x = 0x1.0p-3; // 0.125
. p is used instead of e because e is a hex-digit
Special Floating-Point Values (IEEE 754)
Java follows the IEEE 754 standard for floating-point arithmetic. This standard defines three special values that represent situations where calculations exceed normal numerical limits or produce undefined results:
- The first of these cases is positive infinity: This arises when a number is divided by 0. For Example;
double result = 10.0 / 0;
System.out.println(result); // prints Infinity
This feature is very useful because it allows programs to continue running instead of crushing when overflow occurs. Python would have thrown a ZeroDivision Error however, java is able to handle such scenarios
- The second case occurs when a negative number is divided by zero. For example:
double result = -10.0 / 0;
System.out.println(result); // prints -Infinity
- The third case represented by NaN (Not a number) represents undefined or unrepresentable mathematical results. This include dividind 0 by 0 or the square root of a negative number.For example;
double result1 = 0.0 / 0.0;
double result2 = Math.sqrt(-1);
System.out.println(result1); // prints NaN
System.out.println(result2); // prints NaN
Note: To check if a value is NaN, you could use Java's static methodDouble.isNaN(value)
where value is the result you want to verify. If it returns true then it means it falls under NaN.
double result1 = 0.0 / 0.0; // produces NaN
if (Double.isNaN(result1)) {
System.out.println("Result is not a number!");
}
Top comments (0)