Math 指数 对数 近似值 平方根 三角函数

Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。
其实,Math里面许多数学函数方法是直接调用的StrictMath类里面的方法,而这些函数方法在StrictMath里面的形式是调用本地native方法, 比如:
public static double pow(double a, double b) {
    return StrictMath.pow(a, b); // default impl. delegates to StrictMath
}

public static native double pow(double a, double b);
这里的native方法其实是C代码来实现的,而这些源码在jdk中是没有公布的,根据注释可以获知这些
C的实现来自 ftp://ftp.netlib.org/fdlibm.tar 这里,但是却不能访问,于是百度 netlib 发现在 http://www.netlib.org/liblist.html  下的 fdlibm 中。使用C代码来实现这些基本的函数好处就是提高运行的效率。

各个方法的介绍及案例详见 http://www.yiibai.com/javalang/java_lang_math.html

字段

static double PI    比任何其他值都更接近 pi(即圆的周长与直径之比)的 double 值   
  • 注意:Math 类中对 PI 赋的值为3.1415_92653_58979_32384_6精度为21位】,实际上double的精度只能精确到【3.1415_92653_58979_3,精度为15~16位
  • 之所以这么设计,可能是考虑到以后可能会扩展double类型的精度
static double E    比任何其他值都更接近 e(即自然对数的底数)的 double 值
  • 注意:Math 类中对 E 赋的值为2.7182_81828_45904_52354精度为20位】,实际上double的精度只能精确到【2.7182_81828_45904_5,精度为15~16位
  • 之所以这么设计,可能是考虑到以后可能会扩展double类型的精度
System.out.println(Math.PI+"  "+Math.E);//3.141592653589793  2.718281828459045

近似值:round、ceil、floor、rint 

static double ceil(double a)    返回最小的(最接近负无穷大)double 值,该值大于等于参数,并等于某个整数。 Math.ceil(x) 的值与 -Math.floor(-x) 的值完全相同。
static double floor(double a)    返回最大的(最接近正无穷大)double 值,该值小于等于参数,并等于某个整数。
static double rint(double a)    返回最接近参数并等于某一整数的 double 值。 如果两个同为整数的 double 值都同样接近,那么结果取偶数。
static long round(double a)    返回最接近参数的 long。
static int round(float a)    返回最接近参数的 int。

速记
  • round 附近,返回【四舍五入】后的整数
  • ceil  天花板,返回【大于】参数的最小整数
  • floor 地板 ,返回【小于】参数的最大整数
  • rint  返回【最接近】参数的整数,如果刚好居中,则取偶数

round、ceil、floor、rint 方法经典示例

System.out.println(Math.round(1.5f) + "    " + Math.round(-1.5f));//2 【-1】
System.out.println(Math.round(1.3f) + "    " + Math.round(-1.3f));//1    -1
System.out.println(Math.round(1.6f) + "    " + Math.round(-1.6f) + "\n");//2    -2
//******************************************************************************************
System.out.println(Math.ceil(1.5f) + "    " + Math.ceil(-1.5f));//2.0    -1.0
System.out.println(Math.ceil(1.3f) + "    " + Math.ceil(-1.3f));//2.0    -1.0
System.out.println(Math.ceil(1.6f) + "    " + Math.ceil(-1.6f) + "\n");//2.0    -1.0
//******************************************************************************************
System.out.println(Math.floor(1.5f) + "    " + Math.floor(-1.5f));//1.0    -2.0
System.out.println(Math.floor(1.3f) + "    " + Math.floor(-1.3f));//1.0    -2.0
System.out.println(Math.floor(1.6f) + "    " + Math.floor(-1.6f)+ "\n");//1.0    -2.0
//******************************************************************************************
System.out.println(Math.rint(1.5f) + "    " + Math.rint(-1.5f));//2.0    -2.0
System.out.println(Math.rint(1.3f) + "    " + Math.rint(-1.3f));//1.0    -1.0
System.out.println(Math.rint(1.6f) + "    " + Math.rint(-1.6f));//2.0    -2.0

普通函数

static double/ float/int/long abs(double  a)    返回 double  值的绝对值。
    public static int abs(int a) {
        return (a < 0) ? -a : a;
    }
static double/ float copySign(double magnitude, double sign)    返回带有第二个浮点参数符号的第一个浮点参数。
  • 参数:magnitude - 提供结果数值的参数;sign - 提供结果符号的参数
  • 返回:一个值,带有 magnitude 的数值, sign 的符号。
static double/ float/ int/ long max/ min (double a, double b)    返回两个 double 值中较大/ 的一个。
    public static int max(int a, int b) {
        return (a >= b) ? a : b;
    }
static double/ float nextAfter(double start, double direction)    返回第一个参数和第二个参数之间与第一个参数相邻的浮点数。
static double / float nextUp(double d)    返回 d 和正无穷大之间与 d 相邻的浮点值。
static double / float signum(double d)    返回参数的符号函数;如果参数为 0,则返回 0;如果参数大于 0,则返回 1.0;如果参数小于 0,则返回 -1.0。
static double random()    返回带正号的 double 值,该值大于等于 0.0 且小于 1.0,即 [0.0,1.0) 。

三角函数

static double sin(double a)    返回角的三角正弦。
static double cos(double a)    返回角的三角余弦。
static double tan(double a)    返回角的三角正切。

static double sinh(double x)    返回 double 值的双曲线正弦。
static double cosh(double x)    返回 double 值的双曲线余弦。
static double tanh(double x)    返回 double 值的双曲线余弦。

static double asin(double a)    返回一个值的反正弦;返回的角度范围在 -pi/2 到 pi/2 之间。
static double acos(double a)    返回一个值的反余弦;返回的角度范围在 0.0 到 pi 之间。
static double atan(double a)    返回一个值的反正切;返回的角度范围在 -pi/2 到 pi/2 之间。
static double atan2(double y, double x)    将矩形坐标 (x, y) 转换成极坐标 (r, theta),返回所得角 theta。

角度弧度

static double toDegrees(double angrad)    将用弧度表示的角转换为近似相等的用角度表示的角。
    public static double toDegrees(double angrad) {
        return angrad * 180.0 / PI;
    }
static double toRadians(double angdeg)    将用角度表示的角转换为近似相等的用弧度表示的角。
    public static double toRadians(double angdeg) {
        return angdeg / 180.0 * PI;
    }

指数、对数、平方根

static double cbrt(double a)    返回 double 值的立方根。
static double exp(double a)    返回欧拉数 e 的 double 次幂的值。
static double expm1(double x)    返回 e^x -1。
static double hypot(double x, double y)    返回 sqrt(x^2 +y^2),没有中间溢出或下溢。
static double IEEEremainder(double f1, double f2)    按照 IEEE 754 标准的规定,对两个参数进行余数运算。
static double log(double a)    返回 double 值的自然对数(底数是 e)。
static double log10(double a)    返回 double 值的底数为 10 的对数。
static double log1p(double x)    返回参数与 1 之和的自然对数,等同于log(x+1)
static double pow(double a, double b)    返回第一个参数的第二个参数次幂的值。
static double sqrt(double a)    返回正确舍入的 double 值的正平方根。
static double/float scalb(double d, int scaleFactor)    返回 d × 2^scaleFactor,其舍入方式如同将一个正确舍入的浮点值乘以 double 值集合中的一个值。

其他函数

static int getExponent(double d)    返回 double 表示形式中使用的无偏指数。
static int getExponent(float f)    返回 float 表示形式中使用的无偏指数。
static double ulp(double d)    返回参数的 ulp 大小。
static float ulp(float f)    返回参数的 ulp 大小。

1.8之后新增的方法

static int addExact(int x, int y):返回【x+y】的 ,如果结果溢出int值的范围,则抛出异常。
static long addExact(long x, long y)
//Returns the sum of its arguments, throwing an exception if the result overflows an int.
public static int addExact(int x, int y) {
    int 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("integer overflow");//注意^是异或运算
    return r;
}
static int subtractExact(int x, int y) 返回【x-y】的,如果结果溢出int值的范围,则抛出异常。
static long subtractExact(long x, long 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;
}
static int multiplyExact(int x, int y) 返回【(int)( (long)x * (long)y )】的,如果***
static long multiplyExact(long x, long y)
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;
}
static int incrementExact(int a): 参数值+1】
static long incrementExact(long a)
public static int incrementExact(int a) {
    if (a == Integer.MAX_VALUE) throw new ArithmeticException("integer overflow");
    return a + 1;
}
static int decrementExact(int a): 参数值-1】
static long decrementExact(long a)
public static int decrementExact(int a) {
    if (a == Integer.MIN_VALUE)  throw new ArithmeticException("integer overflow");
    return a - 1;
}
static int negateExact(int a): 参数的相反数】
static long negateExact(long a)
public static int negateExact(int a) {
    if (a == Integer.MIN_VALUE) throw new ArithmeticException("integer overflow");
    return -a;
}
static int toIntExact(long value) 【long类型对应的int类型值
public static int toIntExact(long value) {
    if ((int)value != value) throw new ArithmeticException("integer overflow");
    return (int)value;
}
static int floorDiv(int x, int y): 返回小于或等于【x / y的商】的最大整数值
  • Returns the largest (closest to positive infinity 最接近正无穷大int value that is less than or equal to小于或等于 the algebraic代数 quotient.
static long floorDiv(long x, long 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;
}
static int floorMod(int x, int y)

Returns the floor modulus模 of the int arguments.

The floor modulus is x - (floorDiv(x, y) * y), has the same sign符号  as the divisor除数 y, and is in the range of -abs(y) < r < +abs(y).

The relationship关系 between floorDiv and floorMod is such that:

  • floorDiv(x, y) * y + floorMod(x, y) == x

The difference in values between floorMod and the % operator操作符 is due to由于 the difference between (floorDiv that returns the integer less than or equal to the quotient商) and (the / operator that returns the integer closest to zero).

floorMod和%操作符之间的值之间的差异是由于,【返回的小于或等于商的整数floorDiv】与【返回最接近零的整数的/运算符】之间的差异。

Examples:

  • If the signs of the arguments are the same, the results of floorMod and the % operator are the same.  如果参数的符号相同,则floorMod和%运算符的结果是相同的。
    • floorMod(4, 3) == 1;   and (4 % 3) == 1
  • If the signs of the arguments are different, the results differ from the % operator.  如果参数的符号不同,则结果与%运算符不同。
    • floorMod(+4, -3) == -2;   and (+4 % -3) == +1
    • floorMod(-4, +3) == +2;   and (-4 % +3) == -1
    • floorMod(-4, -3) == -1;   and (-4 % -3) == -1

If the signs of arguments are unknown and a positive正 modulus is needed it can be computed as ( floorMod(x, y)  +  abs(y) )  %  abs(y).

static long floorMod(long x, long y)
public static int floorMod(int x, int y) {
    return x - floorDiv(x, y) * y;
}
static double nextDown(double d):返回与负无穷大方向相邻的d的浮点值。
  • 这个方法语义上等同于nextAfter(d,Double.NEGATIVE_INFINITY); 然而,nextDown实现可能比其等同的nextAfter调用运行得更快。
static float nextDown(float f)
public static double nextDown(double d) {
    if (Double.isNaN(d) || d == Double.NEGATIVE_INFINITY)  return d;
    else {
        if (d == 0.0)  return -Double.MIN_VALUE;
        else  return Double.longBitsToDouble(Double.doubleToRawLongBits(d) + ((d > 0.0d)?-1L:+1L));
    }
}

常用API使用演示

Math.abs(12.3);                 //12.3 返回这个数的绝对值  
Math.abs(-12.3);                //12.3  
  
Math.copySign(1.23, -12.3);     //-1.23,返回第一个参数的量值和第二个参数的符号  
Math.copySign(-12.3, 1.23);     //12.3  
  
Math.signum(x);                 //如果x大于0则返回1.0,小于0则返回-1.0,等于0则返回0  
Math.signum(12.3);              //1.0  
Math.signum(-12.3);             //-1.0  
Math.signum(0);                 //0.0  
  
  
//指数  
Math.exp(x);                    //e的x次幂  
Math.expm1(x);                  //e的x次幂 - 1  
  
Math.scalb(x, y);               //x*(2的y次幂)  
Math.scalb(12.3, 3);            //12.3*2³  
  
//取整  
Math.ceil(12.3);                //返回最近的且大于这个数的整数13.0  
Math.ceil(-12.3);               //-12.0  
  
Math.floor(12.3);               //返回最近的且小于这个数的整数12.0  
Math.floor(-12.3);              //-13.0  
  
//x和y平方和的二次方根  
Math.hypot(x, y);               //√(x²+y²)  
  
//返回概述的二次方根  
Math.sqrt(x);                   //√(x) x的二次方根  
Math.sqrt(9);                   //3.0  
Math.sqrt(16);                  //4.0  
  
//返回该数的立方根  
Math.cbrt(27.0);                //3   
Math.cbrt(-125.0);              //-5  
  
//对数函数  
Math.log(e);                    //1 以e为底的对数  
Math.log10(100);                //10 以10为底的对数  
Math.log1p(x);                  //Ln(x+ 1)  
  
//返回较大值和较小值  
Math.max(x, y);                 //返回x、y中较大的那个数  
Math.min(x, y);                 //返回x、y中较小的那个数  
  
//返回 x的y次幂  
Math.pow(x, y);                   
Math.pow(2, 3);                 //即2³ 即返回:8  
  
//随机返回[0,1)之间的无符号double值  
Math.random();                    
  
//返回最接近这个数的整数,如果刚好居中,则取偶数  
Math.rint(12.3);                //12.0   
Math.rint(-12.3);               //-12.0  
Math.rint(78.9);                //79.0  
Math.rint(-78.9);               //-79.0  
Math.rint(34.5);                //34.0  
Math.rint(35.5);                //36.0  
  
Math.round(12.3);               //与rint相似,返回值为long  
  
//三角函数  
Math.sin(α);                    //sin(α)的值  
Math.cos(α);                    //cos(α)的值  
Math.tan(α);                    //tan(α)的值  
  
//求角  
Math.asin(x/z);                 //返回角度值[-π/2,π/2]  arc sin(x/z)  
Math.acos(y/z);                 //返回角度值[0~π]   arc cos(y/z)  
Math.atan(y/x);                 //返回角度值[-π/2,π/2]  
Math.atan2(y-y0, x-x0);         //同上,返回经过点(x,y)与原点的的直线和经过点(x0,y0)与原点的直线之间所成的夹角  
  
Math.sinh(x);                   //双曲正弦函数sinh(x)=(exp(x) - exp(-x)) / 2.0;  
Math.cosh(x);                   //双曲余弦函数cosh(x)=(exp(x) + exp(-x)) / 2.0;  
Math.tanh(x);                   //tanh(x) = sinh(x) / cosh(x);  
  
//角度弧度互换  
Math.toDegrees(angrad);         //角度转换成弧度,返回:angrad * 180d / PI  
  
Math.toRadians(angdeg);         //弧度转换成角度,返回:angdeg / 180d * PI  
2017-6-9




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值