OverFlow and UnderFlow, Formatting the floating point values


OverFlow and UnderFlow

Float data type

The float data type is a single-precision 32-bit IEEE 754 floating point.

Float overflow

Maximum possible value is 3.4028235e+38 , When it exceeds this value it produces Infinity

float f = 3.4e38f;
float result = f*2; 
System.out.println(result); //Infinity

Float UnderFlow

Minimum value is 1.4e-45f, when is goes below this value it produces 0.0

float f = 1e-45f;
float result = f/1000;
System.out.println(result);

double data type

The double data type is a double-precision 64-bit IEEE 754 floating point.

Double OverFlow

Maximum possible value is 1.7976931348623157e+308 , When it exceeds this value it produces Infinity

double d = 1e308;
double result=d*2; 
System.out.println(result); //Infinity

Double UnderFlow

Minimum value is 4.9e-324, when is goes below this value it produces 0.0

double d = 4.8e-323;
double result = d/1000;
System.out.println(result); //0.0

Formatting the floating point values

Floating point Numbers can be formatted as a decimal number using String.format with 'f' flag

 //Two digits in fracttional part are rounded
 String format1 = String.format("%.2f", 1.2399);
 System.out.println(format1); // "1.24"
 // three digits in fractional part are rounded
 String format2 = String.format("%.3f", 1.2399);
 System.out.println(format2); // "1.240"
 
 //rounded to two digits, filled with zero
 String format3 = String.format("%.2f", 1.2);
 System.out.println(format3); // returns "1.20"
 
 //rounder to two digits
 String format4 = String.format("%.2f", 3.19999);
 System.out.println(format4); // "3.20"

Floating point Numbers can be formatted as a decimal number using DecimalFormat

 // rounded with one digit fractional part
 String format = new DecimalFormat("0.#").format(4.3200);
 System.out.println(format); // 4.3
 
 // rounded with two digit fractional part
 String format = new DecimalFormat("0.##").format(1.2323000);
 System.out.println(format); //1.23
 
 // formatting floating numbers to decimal number
 double dv = 123456789;
 System.out.println(dv); // 1.23456789E8
 String format = new DecimalFormat("0").format(dv);
 System.out.println(format); //123456789

Strict Adherence to the IEEE Specification

By default, floating point operations on float and double do not strictly adhere to the rules of the IEEE 754 specification. An expression is allowed to use implementation-specific extensions to the range of these values; essentially allowing them to be more accurate than required.

strictfp disables this behavior. It is applied to a class, interface, or method, and applies to everything contained in it, such as classes, interfaces, methods, constructors, variable initializers, etc. With strictfp, the intermediate values of a floating-point expression must be within the float value set or the double value set. This causes the results of such expressions to be exactly those that the IEEE 754 specification predicts.

All constant expressions are implicitly strict, even if they aren't inside a strictfp scope.

Therefore, strictfp has the net effect of sometimes making certain corner case computations less accurate, and can also make floating point operations slower (as the CPU is now doing more work to ensure any native extra precision does not affect the result). However, it also causes the results to be exactly the same on all platforms. It is therefore useful in things like scientific programs, where reproducibility is more important than speed.

public class StrictFP { // No strictfp -> default lenient
 
	public strictfp float strict(float input) {
		return input * input / 3.4f; // Strictly adheres to the spec.
		// May be less accurate and may be slower.
	}
 
	public float lenient(float input) {
		return input * input / 3.4f; // Can sometimes be more accurate and faster,
		// but results may not be reproducable.
	}
 
	public static final strictfp class Ops { // strictfp affects all enclosed entities
		private StrictOps() {}
 
		public static div(double dividend, double divisor) { // implicitly strictfp
			return dividend / divisor;
		}
	}
}

Basic Programs