Java教程

Math源码解析

本文主要是介绍Math源码解析,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

里面用到的StrictMath类绝大数都是底层实现的,目前还看不到实现方式,下面就贴出可以看出实现方式的功能

public final class Math{
	private Math() {}
	public static final double E = 2.7182818284590452354;
	public static final double PI = 3.14159265358979323846;
	//以度为单位的角度转换为用弧度表示的近似相等的角度
	public static double toRadians(double angdeg) {
        return angdeg / 180.0 * PI;
    }
    //转换关系跟上面相反
	public static double toDegrees(double angrad) {
        return angrad * 180.0 / PI;
    }
    //取整
    //规则:当X为正数时,第一步先X+0.5,然后取整
    //	   当X为负数时,第一步先X+0.5,第二步观察得到的结果的小数点左边的数为多少,并判断+0.5后得到的数大于0还是小于0,小于0则最后的取整的结果为-(小数点左边的数+1),大于0则取整结果为小数点左边的数
	public static int round(float a) {
        int intBits = Float.floatToRawIntBits(a);
        int biasedExp = (intBits & FloatConsts.EXP_BIT_MASK)
                >> (FloatConsts.SIGNIFICAND_WIDTH - 1);
        int shift = (FloatConsts.SIGNIFICAND_WIDTH - 2
                + FloatConsts.EXP_BIAS) - biasedExp;
        if ((shift & -32) == 0) { 
            int r = ((intBits & FloatConsts.SIGNIF_BIT_MASK)
                    | (FloatConsts.SIGNIF_BIT_MASK + 1));
            if (intBits < 0) {
                r = -r;
            }
            return ((r >> shift) + 1) >> 1;
        } else {
            return (int) a;
        }
    }
    //功能跟上面一样
	public static long round(double a) {
        long longBits = Double.doubleToRawLongBits(a);
        long biasedExp = (longBits & DoubleConsts.EXP_BIT_MASK)
                >> (DoubleConsts.SIGNIFICAND_WIDTH - 1);
        long shift = (DoubleConsts.SIGNIFICAND_WIDTH - 2
                + DoubleConsts.EXP_BIAS) - biasedExp;
        if ((shift & -64) == 0) {
            long r = ((longBits & DoubleConsts.SIGNIF_BIT_MASK)
                    | (DoubleConsts.SIGNIF_BIT_MASK + 1));
            if (longBits < 0) {
                r = -r;
            }
            return ((r >> shift) + 1) >> 1;
        } else {
            return (long) a;
        }
    }
    //位运算溢出抛异常,否则返回x、y的和
	public static int addExact(int x, int y) {
        int r = x + y;
        if (((x ^ r) & (y ^ r)) < 0) {
            throw new ArithmeticException("integer overflow");
        }
        return r;
    }
    //功能跟上面相同
	public static long addExact(long x, long y) {
        long r = x + y;
        // HD 2-12 Overflow iff both arguments have the opposite sign of the result
        if (((x ^ r) & (y ^ r)) < 0) {
            throw new ArithmeticException("long overflow");
        }
        return r;
    }
    //位运算溢出抛异常,否则返回x、y的差
	public static int subtractExact(int x, int y) {
        int r = x - y;
        // HD 2-12 Overflow iff the arguments have different signs and
        // the sign of the result is different than the sign of x
        if (((x ^ y) & (x ^ r)) < 0) {
            throw new ArithmeticException("integer overflow");
        }
        return r;
    }
    //功能跟上面相同
	public static long subtractExact(long x, long y) {
        long r = x - y;
        // HD 2-12 Overflow iff the arguments have different signs and
        // the sign of the result is different than the sign of x
        if (((x ^ y) & (x ^ r)) < 0) {
            throw new ArithmeticException("long overflow");
        }
        return r;
    }
    //如果x、y相乘超出int的范围,则抛异常,否则返回相乘结果
	public static int multiplyExact(int x, int y) {
        long r = (long)x * (long)y;
        if ((int)r != r) {
            throw new ArithmeticException("integer overflow");
        }
        return (int)r;
    }
    //自增1
	public static int incrementExact(int a) {
        if (a == Integer.MAX_VALUE) {
            throw new ArithmeticException("integer overflow");
        }
        return a + 1;
    }
    //功能跟上面相同
	public static long incrementExact(long a) {
        if (a == Long.MAX_VALUE) {
            throw new ArithmeticException("long overflow");
        }

        return a + 1L;
    }
    //自减1
	public static int decrementExact(int a) {
        if (a == Integer.MIN_VALUE) {
            throw new ArithmeticException("integer overflow");
        }

        return a - 1;
    }
    //功能跟上面相同
	public static long decrementExact(long a) {
        if (a == Long.MIN_VALUE) {
            throw new ArithmeticException("long overflow");
        }

        return a - 1L;
    }
   	//自减a
	public static int negateExact(int a) {
        if (a == Integer.MIN_VALUE) {
            throw new ArithmeticException("integer overflow");
        }

        return -a;
    }
    //功能跟上面相同
	public static long negateExact(long a) {
        if (a == Long.MIN_VALUE) {
            throw new ArithmeticException("long overflow");
        }

        return -a;
    }
    //将value强转为int
	public static int toIntExact(long value) {
        if ((int)value != value) {
            throw new ArithmeticException("integer overflow");
        }
        return (int)value;
    }
    //返回不小于x、y相除后的商的最大整数
	public static int floorDiv(int x, int y) {
        int r = x / y;
        // if the signs are different and modulo not zero, round down
        if ((x ^ y) < 0 && (r * y != x)) {
            r--;
        }
        return r;
    }
    //功能跟上面相同
	public static long floorDiv(long x, long y) {
        long r = x / y;
        // if the signs are different and modulo not zero, round down
        if ((x ^ y) < 0 && (r * y != x)) {
            r--;
        }
        return r;
    }
    //返回不大于余数的最大整数
	public static int floorMod(int x, int y) {
        int r = x - floorDiv(x, y) * y;
        return r;
    }
    //功能跟上面相同
	public static long floorMod(long x, long y) {
        return x - floorDiv(x, y) * y;
    }
    //取反
	public static int abs(int a) {
        return (a < 0) ? -a : a;
    }
    //功能跟上面相同
	public static long abs(long a) {
        return (a < 0) ? -a : a;
    }
    //功能跟上面相同
	public static float abs(float a) {
        return (a <= 0.0F) ? 0.0F - a : a;
    }
    //功能跟上面相同
	public static double abs(double a) {
        return (a <= 0.0D) ? 0.0D - a : a;
    }
    //取最大数
	public static int max(int a, int b) {
        return (a >= b) ? a : b;
    }
    //功能跟上面相同
	public static long max(long a, long b) {
        return (a >= b) ? a : b;
    }
	private static long negativeZeroFloatBits  = Float.floatToRawIntBits(-0.0f);
    private static long negativeZeroDoubleBits = Double.doubleToRawLongBits(-0.0d);
    //功能跟上面相同
	public static float max(float a, float b) {
        if (a != a)
            return a;//这个是a为null的情况
        if ((a == 0.0f) &&
            (b == 0.0f) &&
            (Float.floatToRawIntBits(a) == negativeZeroFloatBits)) {
            return b;
        }
        return (a >= b) ? a : b;
    }
    //功能跟上面相同
	public static double max(double a, double b) {
        if (a != a)
            return a;
        if ((a == 0.0d) &&
            (b == 0.0d) &&
            (Double.doubleToRawLongBits(a) == negativeZeroDoubleBits)) {
            return b;
        }
        return (a >= b) ? a : b;
    }
    //取最小数
	public static int min(int a, int b) {
        return (a <= b) ? a : b;
    }
    //功能跟上面相同
	public static long min(long a, long b) {
        return (a <= b) ? a : b;
    }
    //功能跟上面相同
	public static float min(float a, float b) {
        if (a != a)
            return a;   // a is NaN
        if ((a == 0.0f) &&
            (b == 0.0f) &&
            (Float.floatToRawIntBits(b) == negativeZeroFloatBits)) {
            // Raw conversion ok since NaN can't map to -0.0.
            return b;
        }
        return (a <= b) ? a : b;
    }
    //功能跟上面相同
	public static double min(double a, double b) {
        if (a != a)
            return a;   // a is NaN
        if ((a == 0.0d) &&
            (b == 0.0d) &&
            (Double.doubleToRawLongBits(b) == negativeZeroDoubleBits)) {
            // Raw conversion ok since NaN can't map to -0.0.
            return b;
        }
        return (a <= b) ? a : b;
    }
    //如果是0或者NaN,则原样返回,反之返回有符号数值
	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);
    }
}

Math类建议了解日常使用率高的功能即可,其他功能需要的时候回头查找jdk即可,不需要全部了解。
另外没有展示的比较重要的功能就是生成随机数的方法random(),效果是生成0~1的double类型数据

这篇关于Math源码解析的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!