Java StrictMath getExponent()方法与示例
StrictMath类getExponent()方法
语法:
public static int getExponent(float fl);
public static int getExponent(double do);getExponent()方法在java.lang包中可用。
getExponent(floatfl)方法用于返回在给定参数的表示中使用的无偏指数(即,该参数为float类型)。
getExponent(doubledo)方法用于返回在给定参数的表示中使用的无偏指数(即,该参数为double类型)。
这些方法不会引发异常。
这些是静态方法,可以使用类名进行访问,如果尝试使用类对象访问这些方法,则不会出现任何错误。
参数:
float/double-表示要找到其无偏指数的float/double类型值。
返回值:
此方法的返回类型为int/double-返回给定参数的无偏指数,并且参数值为double类型。
注意:
如果我们将NaN作为参数传递,则方法返回(Float.MAX_EXPONENT+1)/(Double.MAX_EXPONENT+1)。
如果我们传递无穷大(正数或负数),则方法返回(Float.MAX_EXPONENT)/(Double.MAX_EXPONENT)。
如果我们传递零,则方法返回(Float.MIN_EXPONENT-1)/(Double.MIN_EXPONENT-1)。
示例
//Java程序演示示例
//getExponent()StrictMath类的方法
public class GetExponent {
public static void main(String[] args) {
//变量声明
float f1 = 7.0f / 0.0f;
float f2 = -7.0f / 0.0f;
float f3 = 0.0f;
float f4 = -0.0f;
float f5 = 12485.2f;
double d1 = 7.0 / 0.0;
double d2 = -7.0 / 0.0;
double d3 = 0.0;
double d4 = -0.0;
double d5 = 12485.2;
//显示f1,f2,f3,f4和f5的先前值
System.out.println("f1: " + f1);
System.out.println("f2: " + f2);
System.out.println("f3: " + f3);
System.out.println("f4: " + f4);
System.out.println("f5: " + f5);
//显示d1,d2,d3,d4和d5的先前值
System.out.println("d1: " + d1);
System.out.println("d2: " + d2);
System.out.println("d3: " + d3);
System.out.println("d4: " + d4);
System.out.println("d5: " + d5);
System.out.println();
System.out.println("getExponent(float): ");
//在这里,我们将得到(Float.MAX_EXPONENT),因为我们
//传递参数,其值为(infinity)
System.out.println("StrictMath.getExponent(f1): " + StrictMath.getExponent(f1));
//在这里,我们将得到(Float.MAX_EXPONENT),因为我们
//传递参数,其值为(-infinity)
System.out.println("StrictMath.getExponent(f2): " + StrictMath.getExponent(f2));
//在这里,我们将得到(Float.MIN_EXPONENT-1),因为我们是
//传递参数,其值为(0.0f)
System.out.println("StrictMath.getExponent(f3): " + StrictMath.getExponent(f3));
//在这里,我们将得到(Float.MIN_EXPONENT-1),因为我们是
//传递参数,其值为(-0.0f)
System.out.println("StrictMath.getExponent(f4): " + StrictMath.getExponent(f4));
//在这里,我们将得到无偏指数,因为我们
//传递参数,其值为(12485.2f)
System.out.println("StrictMath.getExponent(f5): " + StrictMath.getExponent(f5));
System.out.println();
System.out.println("getExponent(double): ");
//在这里,我们将得到(Double.MAX_EXPONENT),因为我们
//传递参数,其值为(infinity)
System.out.println("StrictMath.getExponent(d1): " + StrictMath.getExponent(d1));
//在这里,我们将得到(Double.MAX_EXPONENT),因为我们
//传递参数,其值为(-infinity)
System.out.println("StrictMath.getExponent(d2): " + StrictMath.getExponent(d2));
//在这里,我们将得到(Double.MIN_EXPONENT-1),因为我们是
//传递参数,其值为(0.0)
System.out.println("StrictMath.getExponent(d3): " + StrictMath.getExponent(d3));
//在这里,我们将得到(Double.MIN_EXPONENT-1),因为我们是
//传递参数,其值为(-0.0)
System.out.println("StrictMath.getExponent(d4): " + StrictMath.getExponent(d4));
//在这里,我们将得到无偏指数,因为我们
//传递参数,其值为(12485.2)
System.out.println("StrictMath.getExponent(d5): " + StrictMath.getExponent(d5));
}
}输出结果
f1: Infinity f2: -Infinity f3: 0.0 f4: -0.0 f5: 12485.2 d1: Infinity d2: -Infinity d3: 0.0 d4: -0.0 d5: 12485.2 getExponent(float): StrictMath.getExponent(f1): 128 StrictMath.getExponent(f2): 128 StrictMath.getExponent(f3): -127 StrictMath.getExponent(f4): -127 StrictMath.getExponent(f5): 13 getExponent(double): StrictMath.getExponent(d1): 1024 StrictMath.getExponent(d2): 1024 StrictMath.getExponent(d3): -1023 StrictMath.getExponent(d4): -1023 StrictMath.getExponent(d5): 13