Java getExponent Function

The Java getExponent Function is one of the Math Library functions, which is to return the unbiased exponent used in the representation of double or float value. In this article, we will show how to use Java Math.getExponent function with an example.

Java getExponent Syntax

The basic syntax of the Math.getExponent Function in Java Programming language is as shown below.

Math.getExponent(data_type number);

Number: It can be a number or a valid numerical expression.

  • If the number argument is positive or negative double or float value, Java Math.getExponent function will return the unbiased exponent.
  • If the number argument is not a number(NaN) or Infinite, Math.getExponent function will return Float.MAX_EXPONENT + 1.
  • And if it is Zero, Math.getExponent function will return Float.MIN_EXPONENT – 1.

The mathematical formula behind this Java getExponent function is 2exp <= number (Where exp is the output or result). For example, We want to find the result of Math.getExponent(24.50)

2exp <= 24.50

24 <= 24.50, so the answer will be 4. (If our value is greater than 32, then the output will be 5 because 25 = 32)

Java Programming provides two different functions to find the closest exponent of the specified value.

The following Java getExponent float function will accept positive or negative float value as an argument and returns the closest exponent value of type Int.

static int getExponent(float number); //Return Type is Integer

// In order to use in program: 
Math.getExponent(float number);

The following getExponent double function will accept a positive or negative double value as an argument and returns the closest exponent integer value.

static int getExponent(double number); //Return Type is Integer

// In order to use in program: 
Math.getExponent(double number);

Java getExponent Function Example

The Math getExponent function rounds the number to the nearest exponent value. In this getExponent program, We are going to find the same on both positive and negative values and display the output

package MathFunctions;

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

		double a = Math.getExponent(10.9666 - 14.9865 + 10.9852);
		System.out.println("Math.getExponent Result = = " + a);	

		System.out.println("\nMath.getExponent Result of Positive Number = " + Math.getExponent(17.00));
		System.out.println("Math.getExponent Result of Positive Number = " + Math.getExponent(38.45));
		
		System.out.println("\nMath.getExponent Result of Negative Number = " + Math.getExponent(-20.85));
		System.out.println("Math.getExponent Result of Negative Number = " + Math.getExponent(-140.25));
		
		System.out.println("\nMath.getExponent Result = " + Math.getExponent(1));
		System.out.println("Math.getExponent Result = " + Math.getExponent(-1));
		
	}
}
Java getExponent Function 1

First, We declared a variable of type Double and performed the Java Math.getExponent function directly on the expression.

double a = Math.getExponent(10.9666 - 14.9865 + 10.9852);
System.out.println("Math.getExponent Result = = " + a);

Math.getExponent(10.9666 – 14.9865 + 10.9852) ==> Math.getExponent (6.9653) ==> 2. This is because 2² = 4, which is the closest integer value less than 6.9653

Next, We used the getExponent Math function directly on Positive double values.

System.out.println("\nMath.getExponent Result of Positive Number = " + Math.getExponent(17.00));
System.out.println("Math.getExponent Result of Positive Number = " + Math.getExponent(38.45));

Next, We used the Math.getExponent Function directly on Negative double values.

System.out.println("\nMath.getExponent Result of Negative Number = " + Math.getExponent(-20.85));
System.out.println("Math.getExponent Result of Negative Number = " + Math.getExponent(-140.25));

Java getExponent on Array example

In this Java program, we find the closest exponent values of bulk data. Here, we are going to declare an array of double type and find the closest exponent values of array elements.

package MathFunctions;

public class getExponentMethodOnArrays {
	public static void main(String[] args) {
		
		double [] myArray = {-9.69, 24.98, 38.21, -69.9999, 148.879, 259.97};

		for (int i = 0; i < myArray.length; i++) {
			System.out.println("Math.getExponent Result of Array Element = " + Math.getExponent(myArray[i]));
		}
	}
}
Math.getExponent Result of Array Element = 3
Math.getExponent Result of Array Element = 4
Math.getExponent Result of Array Element = 5
Math.getExponent Result of Array Element = 6
Math.getExponent Result of Array Element = 7
Math.getExponent Result of Array Element = 8

We used Java For Loop to iterate the Array. Within the getexponent For Loop, we initialized the i value as 0. Next, the compiler will check for the condition (i < myArray.length).

TIP: myArray.length finds the length of the Java array.

for (int i = 0; i < myArray.length; i++) {

Here, we used the Math.getExponent Function directly inside the System.out.format statement. Here, the compiler will call the Java getExponent method ( static int getExponent(double number) ) to find the exponent of corresponding values and prints the output.

TIP: If your data is of Float type, the compiler will call the ( static int getExponent(float number) ) to find the closest exponent

System.out.println("Math.getExponent Result of Array Element = " + Math.getExponent(myArray[i]));

NOTE: To find the closet exponent of a single item, use: Math.getExponent(myArray[index_position])