Java – Find Unbiased Exponent of Double – getExponent() Method with Examples

java.lang.Math.getExponent() method is used to find the unbiased exponent used in the representation of a double value for the given input (x – parameter) in Java.

Package

getExponent() method exists in Math class of java.lang package.
Since: JDK1.6

 

Method Declaration of getExponent() Method

The declaration of getExponent() method is:

public static double getExponent(double x)

 

Syntax of getExponent() Method

The syntax of getExponent() method in Java is:

 
java.lang.Math.getExponent(double x)

Example

/*
*Find the unbiased exponent used in the representation of given input(double) without importing the 
java.lang Package.
*/
public class JavaMathExample {
    public static void main(String[] args) {
        //Declaring variables
        double x = -4876.1874;
        double result;
        //Assign unbiased exponent used in the representation of x to result variable
        result = java.lang.Math.getExponent(x);
        //Printing result to the console
        System.out.println(result);
    }
}

Output:

Example
By importing java.lang package the syntax of getExponent() method can be used as:

/*
*Find the unbiased exponent used in the representation of given input(double) by importing the java.lang Package.
*/
import java.lang.*;  //importing package

public class JavaMathExample {
    public static void main(String[] args) {
        
        //Declaring variables
        double x = -68.97;
        double result;
        
        //Assign unbiased exponent used in the representation of x to result variable
        result = Math.getExponent(x);
        
        //Printing result to the console
        System.out.println(result);
    }
}

Output:

 

Special Cases of getExponent() Method in Java

If the argument is NaN or infinite, then the result is Double.MAX_EXPONENT + 1.
If the argument is zero or subnormal, then the result is Double.MIN_EXPONENT -1.
Example

/*
*This example shows some special cases of getExponent() method in Java
*/
import java.lang.*; //importing java.lang package

public class JavaMathExample {
    public static void main(String[] args) {

        //Declaring variables
        double positiveZero = 0;
        double negativeZero = -0;
        double positiveInfinity = Double.POSITIVE_INFINITY;
        double negativeInfinity = Double.NEGATIVE_INFINITY;
        double nan = Double.NaN;
        double result;

        //Here argument is positive zero, output will also be positive zero
        result = Math.getExponent(positiveZero);
        System.out.println(result);

        //Here argument is negative zero, output will be positive zero
        result = Math.getExponent(negativeZero);
        System.out.println(result);

        //Here argument is positive infinity, output will also be positive infinity
        result = Math.getExponent(positiveInfinity);
        System.out.println(result);

        //Here argument is negative infinity, output will be negative infinity
        result = Math.getExponent(negativeInfinity);
        System.out.println(result);

        //Here argument is NaN, output will be NaN
        result = Math.getExponent(nan);
        System.out.println(result);


    }

}

Output:

 

Parameters of getExponent() Method in Java

x – Where x is any valid double input value. This parameter is required.

 

Error Handling

If the x parameter is not a number getExponent() method returns an error.
If there is no argument (x – input value) passes to the method, the compiler will produce an error.Example

/*
*This example returns a compile time error
*/
import java.lang.*; //importing java.lang package

public class JavaMathExample {
    public static void main(String[] args) {

        //Declaring variables
        String x = "Not a number";
        double result;

        //argument is not a number
        result = Math.getExponent(x);
        System.out.println(result);

        //Here no argument is used
        result = Math.getExponent();
        System.out.println(result);


    }

}

Output:
Error

 

Return Value of getExponent() Method in Java

getExponent() method returns the unbiased exponent used in the representation of a double value for the given input (x – parameter).

 

Java getExponent() Method Example 1

import java.lang.*; //importing java.lang package

public class JavaMathExample {
    public static void main(String[] args) {
        double negativeValue = -18.869;
        double positiveValue = 27.78639;
        double zero = 0;
        System.out.println("unbiased exponent used in the representation of a Negative Value: "+Math.getExponent(negativeValue));
        System.out.println("unbiased exponent used in the representation of a Positive Value: "+Math.getExponent(positiveValue));
        System.out.println("unbiased exponent used in the representation of Zero: "+Math.getExponent(zero));

    }
}

Output:

 

Java getExponent() Method Example 2

import java.lang.*; //importing java.lang package

public class JavaMathExample {
    public static void main(String[] args) {
        //Declare and initialize an array containing double values
        double test [] = { Double.MAX_VALUE, Double.MIN_VALUE, 
		Double.NaN, Double.MIN_EXPONENT, 42.867e-12, 16.852, 0, -2.58, -78.8569e7 };

        //Use for loop to print unbiased exponent used in the representation of each element of the array
        for (int i=0; i < test.length; i++) {
            System.out.println(  Math.getExponent(test[i])  );
        }

    }
}

Output: