Java 工具类总结(1): 使基本数值计算更简单 - Math

1. Math 类说明

/**
 * The class Math contains methods for performing basic
 * numeric operations such as the elementary exponential, logarithm,
 * Math 类提供基本的函数, 用于指数, 对数, 平方根, 三角运算.
 * 
 * Unlike some of the numeric methods of class
 * StrictMath, all implementations of the equivalent
 * functions of class Math are not defined to return the
 * bit-for-bit same results.  This relaxation permits
 * better-performing implementations where strict reproducibility is
 * not required. 
 * 相比于 StrictMath 类, Math 类不需要保证高精度, 所以运算效率更高.
 *
 * @author  unascribed
 * @author  Joseph D. Darcy
 * @since   JDK1.0
 */

From JavaSE 8 source code : Math.class


2. 常用的常量与方法


2.1 常量

/**
  * The double value that is closer than any other to
  * e, the base of the natural logarithms.
  *  Math.E 是自然数 e 的近似值, 用于包含自然数的数值运算;
  */
 public static final double E = 2.7182818284590452354;

 /**
  * The double value that is closer than any other to
  * PI, the ratio of the circumference of a circle to its.
  * Math.PI 是圆周率 PI 的近似值, 用于涉及曲线的相关的运算;
  */
 public static final double PI = 3.14159265358979323846;

2.2 常用方法

都是类方法, 可以直接通过类调用, 例如: Math.abs(double d);

第一组: ceil() / floor() / rint() / round()


/**
  * Returns the smallest (closest to negative infinity)
  * {@code double} value that is greater than or equal to the
  * argument and is equal to a mathematical integer. 
  * 返回不小于 double 参数的整数中最小的值, 返回类型是 double;
  * 
  * Special cases:
  * <ul><li>If the argument value is already equal to a
  * mathematical integer, then the result is the same as the
  * argument.  
  * <li>If the argument is NaN or an infinity or
  * positive zero or negative zero, then the result is the same as
  * <li>If the argument value is less than zero but
  * greater than -1.0, then the result is negative zero.</ul> 
  * 如果 double 参数已经等于整数, 则返回值本身; 如果 double 参数是 NaN / 正无穷 / 负无穷, 则返回值本身;
  * 如果 double 参数小于 0, 大于 -1, 则返回 0;
  *
  * Note that the value of {@code Math.ceil(x)} is exactly the
  * value of {@code -Math.floor(-x)}.
  * Math.ceil(x) 的作用与 -Math.floor(-x) 等效
  *
  * @param   a   a value.
  * @return  the smallest (closest to negative infinity)
  *          floating-point value that is greater than or equal to
  *          the argument and is equal to a mathematical integer.
  */
    public static double ceil(double a) {
        return StrictMath.ceil(a); // default impl. delegates to StrictMath
    }

    /**
     * Returns the largest (closest to positive infinity)
     * {@code double} value that is less than or equal to the
     * argument and is equal to a mathematical integer. Special cases:
     * <ul><li>If the argument value is already equal to a
     * mathematical integer, then the result is the same as the
     * argument.  <li>If the argument is NaN or an infinity or
     * positive zero or negative zero, then the result is the same as
     * the argument.</ul>
     * 相较于 ceil() 方法, floor() 方法是: 
     * 回不大于 double 参数的整数中最大的值, 返回类型是 double;
     * 
     * @param   a   a value.
     * @return  the largest (closest to positive infinity)
     *          floating-point value that less than or equal to the argument
     *          and is equal to a mathematical integer.
     */
    public static double floor(double a) {
        return StrictMath.floor(a); // default impl. delegates to StrictMath
    }

    /**
     * Returns the {@code double} value that is closest in value
     * to the argument and is equal to a mathematical integer. If two
     * {@code double} values that are mathematical integers are
     * equally close, the result is the integer value that is even.
     * 数学中的, 四舍五入方法; 
     *  
     * Special cases:
     * <ul><li>If the argument value is already equal to a mathematical
     * integer, then the result is the same as the argument.
     * <li>If the argument is NaN or an infinity or positive zero or negative
     * zero, then the result is the same as the argument.</ul>
     * 同上
     * 
     * @param   a   a {@code double} value.
     * @return  the closest floating-point value to {@code a} that is
     *          equal to a mathematical integer.
     */
    public static double rint(double a) {
        return StrictMath.rint(a); // default impl. delegates to StrictMath
    }

    /**
     * Returns the closest {@code int} to the argument, with ties
     * rounding to positive infinity.
     * 四舍五入方法;
     *
     * <p>
     * Special cases:
     * <ul><li>If the argument is NaN, the result is 0.
     * <li>If the argument is negative infinity or any value less than or
     * equal to the value of {@code Integer.MIN_VALUE}, the result is
     * equal to the value of {@code Integer.MIN_VALUE}.
     * <li>If the argument is positive infinity or any value greater than or
     * equal to the value of {@code Integer.MAX_VALUE}, the result is
     * equal to the value of {@code Integer.MAX_VALUE}.</ul>
     *
     * @param   a   a floating-point value to be rounded to an integer.
     * @return  the value of the argument rounded to the nearest
     *          {@code int} value.
     * @see     java.lang.Integer#MAX_VALUE
     * @see     java.lang.Integer#MIN_VALUE
     */
    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) { // shift >= 0 && shift < 32
            // a is a finite number such that pow(2,-32) <= ulp(a) < 1
            int r = ((intBits & FloatConsts.SIGNIF_BIT_MASK)
                    | (FloatConsts.SIGNIF_BIT_MASK + 1));
            if (intBits < 0) {
                r = -r;
            }
            // In the comments below each Java expression evaluates to the value
            // the corresponding mathematical expression:
            // (r) evaluates to a / ulp(a)
            // (r >> shift) evaluates to floor(a * 2)
            // ((r >> shift) + 1) evaluates to floor((a + 1/2) * 2)
            // (((r >> shift) + 1) >> 1) evaluates to floor(a + 1/2)
            return ((r >> shift) + 1) >> 1;
        } else {
            // a is either
            // - a finite number with abs(a) < exp(2,FloatConsts.SIGNIFICAND_WIDTH-32) < 1/2
            // - a finite number with ulp(a) >= 1 and hence a is a mathematical integer
            // - an infinity or NaN
            return (int) a;
        }
    }

    /**
     * Returns the closest {@code long} to the argument, with ties
     * rounding to positive infinity.
     * 四舍五入方法
     * <p>Special cases:
     * <ul><li>If the argument is NaN, the result is 0.
     * <li>If the argument is negative infinity or any value less than or
     * equal to the value of {@code Long.MIN_VALUE}, the result is
     * equal to the value of {@code Long.MIN_VALUE}.
     * <li>If the argument is positive infinity or any value greater than or
     * equal to the value of {@code Long.MAX_VALUE}, the result is
     * equal to the value of {@code Long.MAX_VALUE}.</ul>
     *
     * @param   a   a floating-point value to be rounded to a
     *          {@code long}.
     * @return  the value of the argument rounded to the nearest
     *          {@code long} value.
     * @see     java.lang.Long#MAX_VALUE
     * @see     java.lang.Long#MIN_VALUE
     */
    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) { // shift >= 0 && shift < 64
            // a is a finite number such that pow(2,-64) <= ulp(a) < 1
            long r = ((longBits & DoubleConsts.SIGNIF_BIT_MASK)
                    | (DoubleConsts.SIGNIF_BIT_MASK + 1));
            if (longBits < 0) {
                r = -r;
            }
            // In the comments below each Java expression evaluates to the value
            // the corresponding mathematical expression:
            // (r) evaluates to a / ulp(a)
            // (r >> shift) evaluates to floor(a * 2)
            // ((r >> shift) + 1) evaluates to floor((a + 1/2) * 2)
            // (((r >> shift) + 1) >> 1) evaluates to floor(a + 1/2)
            return ((r >> shift) + 1) >> 1;
        } else {
            // a is either
            // - a finite number with abs(a) < exp(2,DoubleConsts.SIGNIFICAND_WIDTH-64) < 1/2
            // - a finite number with ulp(a) >= 1 and hence a is a mathematical integer
            // - an infinity or NaN
            return (long) a;
        }
    }

演示 demo:

// 不小于 param 的最小整数
System.out.println(Math.ceil(2.3)); // 3.0
System.out.println(Math.ceil(2.7)); // 3.0
System.out.println(Math.ceil(-2.3));    // -2.0
System.out.println(Math.ceil(-2.7));    // -2.0

// 不大于 param 的最大整数
System.out.println(Math.floor(2.3));    // 2.0
System.out.println(Math.floor(2.7));    // 2.0
System.out.println(Math.floor(-2.3));   // -3.0
System.out.println(Math.floor(-2.7));   // -3.0

// 四舍五入: rint() / round(float) / round(double)
// rint() 注意点(暂时无法解释):
// 当小数点前一位是偶数的时候, 舍去小数位; 如, Math.rint(2.5) = 2.0
// 当小数点前一位是奇数的时候, 进一位; 如, Math.rint(3.5) = 4.0
// 暂时, 不推荐使用
System.out.println(Math.rint(2.5)); // 2.0
System.out.println(Math.rint(-2.5));    // -2.0
System.out.println(Math.rint(3.5)); // 4.0
System.out.println(Math.rint(-3.5));    // -4.0

//round(float)
// 暂时不推荐使用
System.out.println(Math.round(2.5f));   //3
System.out.println(Math.round(-2.5f));  //-2
System.out.println(Math.round(3.5f));   //4
System.out.println(Math.round(-3.5f));  //-3
System.out.println(Math.round(-4.5));   //-4

结论:
1. Math.floor() / Math.ceil(): 向下取整/ 向上取整;
2. Math.rint() / Math.round() : 四舍五入, 不推荐使用; (暂时不太明白, 为什么会出现上面的结果)


第二组: random()

private static final class RandomNumberGeneratorHolder {
    static final Random randomNumberGenerator = new Random();
}

/**
 * Returns a {@code double} value with a positive sign, greater
 * than or equal to {@code 0.0} and less than {@code 1.0}.
 * Returned values are chosen pseudorandomly with (approximately)
 * uniform distribution from that range.
 * 返回一个返回在 [0.0, 1.0] 之间的伪随机数 double 类型;
 * 
 * <p>When this method is first called, it creates a single new
 * pseudorandom-number generator, exactly as if by the expression
 * <blockquote>{@code new java.util.Random()}</blockquote>
 * This new pseudorandom-number generator is used thereafter for
 * all calls to this method and is used nowhere else.
 *
 * <p>This method is properly synchronized to allow correct use by
 * more than one thread. However, if many threads need to generate
 * pseudorandom numbers at a great rate, it may reduce contention
 * for each thread to have its own pseudorandom-number generator.
 * 当第一次调用 random() 方法的时候, 系统会生成一个伪随机类, 用于被所有的线程调用;
 * 当线程较少的时候, 所有线程都会使用这个伪随机类生成随机数; 但是一旦访问密集的时候,
 * 特定的线程就会重新生成自己的伪随机列;
 * 
 * 总结: 此方法, 是线程安全的 (个人理解)
 * @return  a pseudorandom {@code double} greater than or equal
 * to {@code 0.0} and less than {@code 1.0}.
 * @see Random#nextDouble()
 */
public static double random() {
    return RandomNumberGeneratorHolder.randomNumberGenerator.nextDouble();
}

显示 demo :

// 单线程演示
// random()
for(int i=0; i<10; ++i) {
    System.out.println(Math.random());
}

// 多线程显示
// todo... ...

2.3 案例分析

todo…


2.4 所有类方法

类方法简洁说明
java.lang.Math.sin(double)
java.lang.Math.cos(double)
java.lang.Math.tan(double)
java.lang.Math.asin(double)
java.lang.Math.acos(double)
java.lang.Math.atan(double)
java.lang.Math.toRadians(double)
java.lang.Math.toDegrees(double)
java.lang.Math.exp(double)
java.lang.Math.log(double)
java.lang.Math.log10(double)
java.lang.Math.sqrt(double)
java.lang.Math.cbrt(double)
java.lang.Math.IEEEremainder(double, double)
java.lang.Math.ceil(double)向上取整
java.lang.Math.floor(double)向下取整
java.lang.Math.rint(double)
java.lang.Math.atan2(double, double)
java.lang.Math.pow(double, double)
java.lang.Math.round(float)
java.lang.Math.round(double)
java.lang.Math.RandomNumberGeneratorHolder
java.lang.Math.random()
java.lang.Math.addExact(int, int)
java.lang.Math.addExact(long, long)
java.lang.Math.subtractExact(int, int)
java.lang.Math.subtractExact(long, long)
java.lang.Math.multiplyExact(int, int)
java.lang.Math.multiplyExact(long, long)
java.lang.Math.incrementExact(int)
java.lang.Math.incrementExact(long)
java.lang.Math.decrementExact(int)
java.lang.Math.decrementExact(long)
java.lang.Math.negateExact(int)
java.lang.Math.negateExact(long)
java.lang.Math.toIntExact(long)
java.lang.Math.floorDiv(int, int)
java.lang.Math.floorDiv(long, long)
java.lang.Math.floorMod(int, int)
java.lang.Math.floorMod(long, long)
java.lang.Math.abs(int)
java.lang.Math.abs(long)
java.lang.Math.abs(float)
java.lang.Math.abs(double)
java.lang.Math.max(int, int)
java.lang.Math.max(long, long)
java.lang.Math.negativeZeroFloatBits
java.lang.Math.negativeZeroDoubleBits
java.lang.Math.max(float, float)
java.lang.Math.max(double, double)
java.lang.Math.min(int, int)
java.lang.Math.min(long, long)
java.lang.Math.min(float, float)
java.lang.Math.min(double, double)
java.lang.Math.ulp(double)
java.lang.Math.ulp(float)
java.lang.Math.signum(double)
java.lang.Math.signum(float)
java.lang.Math.sinh(double)
java.lang.Math.cosh(double)
java.lang.Math.tanh(double)
java.lang.Math.hypot(double, double)
java.lang.Math.expm1(double)
java.lang.Math.log1p(double)
java.lang.Math.copySign(double, double )
java.lang.Math.copySign(float, float)
java.lang.Math.getExponent(float)
java.lang.Math.getExponent(double)
java.lang.Math.nextAfter(double, double)
java.lang.Math.nextAfter(float, double)
java.lang.Math.nextUp(double)
java.lang.Math.nextUp(float)
java.lang.Math.nextDown(double)
java.lang.Math.nextDown(float)
java.lang.Math.scalb(double, int)
java.lang.Math.scalb(float, int)
java.lang.Math.twoToTheDoubleScaleUp
java.lang.Math.twoToTheDoubleScaleDown
java.lang.Math.powerOfTwoD(int)
java.lang.Math.powerOfTwoF(int)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/** * @project: WebProjectUtil * @class: NumberUtil * @describe: 此工具类用来处理数字方面的逻辑, * 如返回指定位数的随机数字、Double的加减乘除精确运算、指定位数数字用“0”补齐 * @autho: Administrator * @date: 2013-6-7 下午02:26:27 * @alter: Administrator * @alterDate: 2013-6-7 下午02:26:27 * @alterRemark: * @version V1.0 */ public class NumberUtil { private static final int DEF_DIV_SCALE = 2; /** * @return 返回12位随机数 */ public static String randomNumber() { } /** * @param parm * @return 返回指定位数随机数 */ public static String randomNumber(int parm) { } /** * * 两个Double数相加 * * @param v1 * @param v2 * @return Double */ public static Double add(Double v1, Double v2) { } /** * * 两个Double数相减 * * @param v1 * @param v2 * @return Double */ public static Double sub(Double v1, Double v2) { } /** * * 两个Double数相乘 * * @param v1 * @param v2 * @return Double */ public static Double mul(Double v1, Double v2) { } /** * * 两个Double数相除 * * @param v1 * @param v2 * @return Double */ public static Double div(Double v1, Double v2) { } /** * * 两个Double数相除,并保留scale位小数 * * @param v1 * @param v2 * @param scale * @return Double */ public static Double div(Double v1, Double v2, int scale) { } /** * 返回指定Double的负数 * @param v1 * @return */ public static Double neg(Double v1) { /** * @Title: toFixdLengthString * @Description: 将字符串用符号填充位数 * @param str 源字符串 * @param fixdlenth 位数 * @return String * @throws */ public static String toFixdLengthString(String str, int fixdlenth) { } /** * @Title: toFixdLengthString * @Description: 将数字用“0”填充位数 * @param num * @param fixdlenth * @return String * @throws */ public static String toFixdLengthString(int num, int fixdlenth) { } /** * @Title: generateSpaceString * @Description: 得到指定位数占位符 * @param length * @return String * @throws */ public static String generateSpaceString(int length) { } /** * @Title: generateZeroString * @Description: 得到指定位数的“0”的占位符 * @param length * @return String * @throws */ public static String generateZeroString(int length) { } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值