What is the Math.getExponent() method in Java?

The static method getExponent() is used to get the unbiased exponent used in the demonstration of a number (double or float).

Syntax

For double-type values:


static int getExponent(double x)

For floating-point values:


static int getExponent(float x)

Parameters

x: This is an argument value of either double or float type.

Return value

The getExponent() function from the math library returns the unbiased exponent of the argument x.

Special cases

  • The result will be Double.MAX_EXPONENT + 1 or Float.MAX_EXPONENT + 1 when the argument value is infinite or NaN.

  • The result will be Double.MIN_EXPONENT -1 or Float.MIN_EXPONENT - 1 when the argument value is subnormal or zero.

  • Double.MAX_EXPONENT = 1023
  • Double.MIN_EXPONENT = -1022
  • Float.MAX_EXPONENT = 127
  • Float.MIN_EXPONENT = -126

Code

In the code snippet below:

  • Case 1: When the argument value is a positive number, it will show an unbiased exponent.
  • Case 2: When an argument is of infinite value, then the result will be Double.MAX_EXPONENT + 1 or Float.MAX_EXPONENT + 1.
  • Case 3: When an argument is zero, then the result will be Double.MIN_EXPONENT -1 or Float.MIN_EXPONENT - 1.
import java.lang.Math;
// Main class
class EdPresso {
public static void main( String args[] ) {
double num1=10;
float num2=1024;
System.out.println("Double type num1: " + Math.getExponent(num1));
System.out.println("Float type num2: " + Math.getExponent(num2));
}
}

Free Resources