深入学习java源码之Math.ulp()与 Math.signum()
原创
©著作权归作者所有:来自51CTO博客作者wespten的原创作品,请联系作者获取转载授权,否则将追究法律责任
深入学习java源码之Math.ulp()与 Math.signum()
swtich()变量类型只能是int、short、char、byte和enum类型(JDK 1.7 之后,类型也可以是String了)。当进行case判断时,JVM会自动从上到小扫描,寻找匹配的case,可能存在以下情况:
当每一个case都不存在break时,匹配成功后,从当前case开始,依次返回后续所有case的返回值。
int i = 2;
switch(i){
case 0:
System.out.println("0");
case 1:
System.out.println("1");
case 2:
System.out.println("2");
default:
System.out.println("default");
}
输出:2
default
若当前匹配成功的case不存在break,则从当前case开始,依次返回后续case的返回值,直到遇到break,跳出判断。
int i = 2;
switch(i){
case 0:
System.out.println("0");
case 1:
System.out.println("1");
case 2:
System.out.println("2");
case 3:
System.out.println("3");break;
default:
System.out.println("default");
}
输出:2
3
default的运用,是当switch语句里,所有的case语句都不满足条件时,则执行default语句
default在switch开头:
若所有case都不满足条件,则执行default语句,并执行default语句之后的case语句,直到break或结束
default在switch中间:(同上)
若所有case都不满足条件,则执行default语句,并执行default语句之后的case语句,直到break或结束
default在switch末尾:
若所有case语句都不满足条件,则执行default语句,结束;若有case满足,则执行case语句直到遇到break或switch语句结束,所以default在最后一行时break可以省略不写(但是不建议省略,以求严谨)
switch语句当中,用于终止语句
getDetail:function(type,Id){
var param = {
id:Id
}
var url = "";
switch(type)
{
case 0:
url = "";
break;
case 1:
url = "/";
break;
}
}
Modifier and Type
| Method and Description
|
static int | getExponent(double d) 返回a的表示中使用的无偏指数 double 。 |
static int | getExponent(float f) 返回a的表示中使用的无偏指数 float 。 |
static double | signum(double d) 返回参数的signum函数; 如果参数为零,则为零,如果参数大于零则为1.0,如果参数小于零,则为-1.0。 |
static float | signum(float f) 返回参数的signum函数; 如果参数为零,则为零,如果参数大于零则为1.0f,如果参数小于零,则为-1.0f。 |
static double | ulp(double d) 返回参数的ulp的大小。 |
static float | ulp(float f) 返回参数的ulp的大小。 |
java源码
public final class Math {
private Math() {}
public static int getExponent(float f) {
/*
* Bitwise convert f to integer, mask out exponent bits, shift
* to the right and then subtract out float's bias adjust to
* get true exponent value
*/
return ((Float.floatToRawIntBits(f) & FloatConsts.EXP_BIT_MASK) >>
(FloatConsts.SIGNIFICAND_WIDTH - 1)) - FloatConsts.EXP_BIAS;
}
public static int getExponent(double d) {
/*
* Bitwise convert d to long, mask out exponent bits, shift
* to the right and then subtract out double's bias adjust to
* get true exponent value.
*/
return (int)(((Double.doubleToRawLongBits(d) & DoubleConsts.EXP_BIT_MASK) >>
(DoubleConsts.SIGNIFICAND_WIDTH - 1)) - DoubleConsts.EXP_BIAS);
}
public static double ulp(double d) {
int exp = getExponent(d);
switch(exp) {
case DoubleConsts.MAX_EXPONENT+1: // NaN or infinity
return Math.abs(d);
case DoubleConsts.MIN_EXPONENT-1: // zero or subnormal
return Double.MIN_VALUE;
default:
assert exp <= DoubleConsts.MAX_EXPONENT && exp >= DoubleConsts.MIN_EXPONENT;
// ulp(x) is usually 2^(SIGNIFICAND_WIDTH-1)*(2^ilogb(x))
exp = exp - (DoubleConsts.SIGNIFICAND_WIDTH-1);
if (exp >= DoubleConsts.MIN_EXPONENT) {
return powerOfTwoD(exp);
}
else {
// return a subnormal result; left shift integer
// representation of Double.MIN_VALUE appropriate
// number of positions
return Double.longBitsToDouble(1L <<
(exp - (DoubleConsts.MIN_EXPONENT - (DoubleConsts.SIGNIFICAND_WIDTH-1)) ));
}
}
}
public static float ulp(float f) {
int exp = getExponent(f);
switch(exp) {
case FloatConsts.MAX_EXPONENT+1: // NaN or infinity
return Math.abs(f);
case FloatConsts.MIN_EXPONENT-1: // zero or subnormal
return FloatConsts.MIN_VALUE;
default:
assert exp <= FloatConsts.MAX_EXPONENT && exp >= FloatConsts.MIN_EXPONENT;
// ulp(x) is usually 2^(SIGNIFICAND_WIDTH-1)*(2^ilogb(x))
exp = exp - (FloatConsts.SIGNIFICAND_WIDTH-1);
if (exp >= FloatConsts.MIN_EXPONENT) {
return powerOfTwoF(exp);
}
else {
// return a subnormal result; left shift integer
// representation of FloatConsts.MIN_VALUE appropriate
// number of positions
return Float.intBitsToFloat(1 <<
(exp - (FloatConsts.MIN_EXPONENT - (FloatConsts.SIGNIFICAND_WIDTH-1)) ));
}
}
}
public static double signum(double d) {
return (d == 0.0 || Double.isNaN(d))?d:copySign(1.0, d);
}
public static float signum(float f) {
return (f == 0.0f || Float.isNaN(f))?f:copySign(1.0f, f);
}
}
public final class StrictMath {
private StrictMath() {}
public static double ulp(double d) {
return Math.ulp(d);
}
public static float ulp(float f) {
return Math.ulp(f);
}
public static double signum(double d) {
return Math.signum(d);
}
public static float signum(float f) {
return Math.signum(f);
}
public static int getExponent(float f) {
return Math.getExponent(f);
}
public static int getExponent(double d) {
return Math.getExponent(d);
}
}