Understanding the getExponent(double d) Method in Java: A Comprehensive Guide
Table of Content:
Description
In this tutorial, we will be showing a java example on how to use the getExponent() method of Math Class. The getExponent() returns the unbiased exponent used in the representation of a double. The following are special cases that must be taken into consideration.
- 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.
Most of the methods of the Math class is static and the getExponent() method is no exception. Thus don’t forget that in order to call this method, you don’t have to create a new object. Use the method in the format Math.getExponent(double d).
Method Syntax
public static int getExponent(double d)
Method Returns
The getExponent() method returns the unbiased exponent of the argument.
Compatibility
Requires Java 1.6 and up
Example
Below is a java code demonstrates the use of getExponent() method of Math class. The example presented might be simple however it shows the behavior of the getExponent() method.
import java.util.Scanner; /* * This example source code demonstrates the use of * getExponent() method of Math class */ public class MathGetExponent { public static void main(String[] args) { // Ask for user input System.out.print("Enter an input:"); // use scanner to read the console input Scanner scan = new Scanner(System.in); // Assign the user to String variable String s = scan.nextLine(); // close the scanner object scan.close(); // convert the string input to double double value = Double.parseDouble(s); // get the result of decrementExact int exponent = Math.getExponent(value); System.out.println("Result of the operation is " + exponent); } }
output
Below is the sample output when you run the above example.
Enter an input:36 Result of the operation is 5 Press any key to continue . . .
The above java example source code demonstrates the use of getExponent() method of Math class. We simply ask for a user input and we use the Scanner class to parse it. Since we have used the nextLine() method to get the console value which is having a return data type of String thus we have used the Double.parseDouble() convert the string input to double. This conversion is required because the argument for getExponent() method only accepts double.