Open In App

Java Math getExponent() Method

Last Updated : 13 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Java, the Math.getExponent() method returns the exponent of the floating-point number in its binary scientific notation. In this article, we are going to discuss how this method works for regular values and for special cases such as infinity and NaN.

Note: This method works with both double and float types also, the number is represented in binary format, and this method gives us the exponent part of the binary representation.

Special Cases:

  • If the argument is NaN or infinity, then the result will be Double.MAX_EXPONENT + 1 or Float.MAX_EXPONENT + 1.
  • If the argument is zero or a very small number, then the result will be Double.MAX_EXPONENT - 1 or Float.MAX_EXPONENT - 1.

These special cases make sure that the Math.getExponent() methods work correctly.

Syntax of getExponent() Method

public static int getExponent(double d)

  • Parameter: This method takes a single parameter d, of type double value whose exponent is to be returned.
  • Return Type: This method returns the exponent part of a floating point number as an integer.

Now, we are going to discuss some examples for better understanding.


Examples of Java Math getExponent() Method

Example 1: In this example, we will see the basic usage of getExponent() method.

Java
// Java program to demonstrate working of
// Math.getExponent() method with regular values
import java.lang.Math;

public class Geeks {
    
    public static void main(String[] args) {
        
        // Declaring some double values
        double a = 12345.67;
        double b = 0.000000123;
        double c = 1000000.0;

        // Using getExponent() to get the 
        // exponent of each number
        System.out.println("Exponent of " + a + ": " + Math.getExponent(a));
        System.out.println("Exponent of " + b + ": " + Math.getExponent(b));
        System.out.println("Exponent of " + c + ": " + Math.getExponent(c));
    }
}

Output
Exponent of 12345.67: 13
Exponent of 1.23E-7: -23
Exponent of 1000000.0: 19


Example 2: In this example, we will see how getExponent() method handles NaN and Infinity.

Java
// Java program to demonstrate
// handling NaN and Infinity
import java.lang.Math;

public class Geeks {
    
    public static void main(String[] args) {
        
        // Declaring NaN and Infinity values
        double n = Double.NaN;
        double i = Double.POSITIVE_INFINITY;

        // Using getExponent() to get the 
        // exponent of NaN and Infinity
        System.out.println("Exponent of NaN: " + Math.getExponent(n));
        System.out.println("Exponent of Infinity: " + Math.getExponent(i));
    }
}

Output
Exponent of NaN: 1024
Exponent of Infinity: 1024

Explanation: Here, we are handling the cases like NaN and Infinity. For NaN it is returning 1024 and for Infinity it is returning 1024.

Important Points:

  • This method is very useful when we have to deal with binary representation of floating point numbers.
  • This method also used when we have to compare large and small numbers in scientific computations.
  • It works for both float and double values.

Practice Tags :

Similar Reads