java.lang.Math.getExponent()
method is used to find the unbiased exponent used in the representation of a float 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 float getExponent(float x)
Syntax of getExponent()
Method
The syntax of
getExponent()
method in Java is:
java.lang.Math.getExponent(float x)
Example
/* *Find the unbiased exponent used in the representation of given input(float) without importing the java.lang Package. */ public class JavaMathExample { public static void main(String[] args) { //Declaring variables float x = -4876.1874; float 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(float) by importing the java.lang Package. */ import java.lang.*; //importing package public class JavaMathExample { public static void main(String[] args) { //Declaring variables float x = -68.97; float 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 Float.MAX_EXPONENT + 1.
If the argument is zero or subnormal, then the result is Float.MIN_EXPONENT -1
Example
If the argument is zero or subnormal, then the result is Float.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 float positiveZero = 0; float negativeZero = -0; float positiveInfinity = Float.POSITIVE_INFINITY; float negativeInfinity = Float.NEGATIVE_INFINITY; float nan = Double.NaN; float 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 float input value. This parameter is required.
Error Handling
If the
If there is no argument (
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"; float 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 float 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) { float negativeValue = -18.869; float positiveValue = 27.78639; float 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 float values float test [] = { Float.MAX_VALUE, Float.MIN_VALUE, Float.NaN, Float.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: