您的位置:首页 > 编程语言 > Java开发

java中Math中的一些属性和方法

2013-10-06 17:58 267 查看
一、Math中的属性:

Math.PI 记录的圆周率,其值为:3.14159265358979323846

Math.E记录e的常量,其值为:2.7182818284590452354

二、Math中的方法:

1)Math.abs(参数1):

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;

}

2)Math.ceil(参数1):ceil天花板的意思,就是返回大的值,得到不小于某数的最大整数,即向上取整,注意一些特殊值


public static double ceil(double a) {

return StrictMath.ceil(a); // default impl. delegates to StrictMath

}

在StrictMath中的代码:
public static double floor(double a) {

return floorOrCeil(a, -1.0, 0.0, -1.0);

}

StrictMath中的floorOrCeil()方法

private static double floorOrCeil(double a,double negativeBoundary,double positiveBoundary,double sign) {

int exponent = Math.getExponent(a);

if (exponent < 0) {

/*

* Absolute value of argument is less than 1.

* floorOrceil(-0.0) => -0.0

* floorOrceil(+0.0) => +0.0

*/

return ((a == 0.0) ? a : ( (a < 0.0) ? negativeBoundary : positiveBoundary) );

} else if (exponent >= 52) {

/*

* Infinity, NaN, or a value so large it must be integral.

*/

return a;

}

// Else the argument is either an integral value already XOR it has to be rounded to one.

assert exponent >= 0 && exponent <= 51;

long doppel = Double.doubleToRawLongBits(a);

long mask = DoubleConsts.SIGNIF_BIT_MASK >> exponent;

if ( (mask & doppel) == 0L )

return a; // integral value

else {

double result = Double.longBitsToDouble(doppel & (~mask));

if (sign*a > 0.0)

result = result + sign;

return result;

}

}

3)Math.floor(参数1):floor地板的意思,就是返回小的值,得到不大于某数的最大整数,即向下取整。
public static double floor(double
a) {

return StrictMath.floor(a); // default impl. delegates to StrictMath

}

public static double floor(double
a) {

return floorOrCeil(a, -1.0, 0.0, -1.0);

}
4)Math.round(参数1):四舍五入,float时返回int值,double时返回long值
public static int round(float
a) {

if (a != 0x1.fffffep-2f) // greatest float value less than 0.5

return (int)floor(a + 0.5f);

else

return 0;

}
public static long round(double
a) {

if (a != 0x1.fffffffffffffp-2) // greatest double value less than 0.5

return (long)floor(a + 0.5d);

else

return 0;

}

5)Math.rint(参数1) rint 四舍五入,返回double值,*
注意.5的时候会取偶数 【求距离某数最近的整数(可能比某数大,也可能比它小)】
public static double rint(double
a) {

return StrictMath.rint(a); // default impl. delegates to StrictMath

}

public static double rint(double
a) {

double twoToThe52 = (double)(1L << 52); // 2^52

double sign = FpUtils.rawCopySign(1.0, a); // preserve sign info

a = Math.abs(a);

if (a < twoToThe52) { // E_min <= ilogb(a) <= 51

a = ((twoToThe52 + a ) - twoToThe52);

}

return sign * a; // restore original sign

}
6).Math.rand():random 取得一个大于或者等于0.0小于不等于1.0的随机数
7)Math.max(参数1,参数2):输出两个数中最大的那个。

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.floatToIntBits(-0.0f);

private static long negativeZeroDoubleBits = Double.doubleToLongBits(-0.0d);

public static float max(float a, float b) {

if (a != a) return a; //
a is NaN

if ((a == 0.0f) && (b == 0.0f)

&& (Float.floatToIntBits(a) == negativeZeroFloatBits)) {

return b;

}

return (a >= b) ? a : b;

}

public static double max(double a, double b) {

if (a != a) return a; //
a is NaN

if ((a == 0.0d) && (b == 0.0d)

&& (Double.doubleToLongBits(a) == negativeZeroDoubleBits)) {

return b;

}

return (a >= b) ? a : b;

}
8)Math.min(参数1,参数2):输出两个数中最小的哪个。

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.floatToIntBits(b) == negativeZeroFloatBits)) {

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.doubleToLongBits(b) == negativeZeroDoubleBits)) {

return b;

}

return (a <= b) ? a : b;

}

9)Math.IEEEremainder(f1, f2):求余

Math.pow(x,y):x的y次方 (求某数的任意次方, 抛出ArithmeticException处理溢出异常)

Math.sqrt(x):平方根

Math.exp(x) 求e的任意次方

Math.log10(x) 以10为底的对数

Math.log(x)自然对数

10)三角函数中的方法:
Math.sin()
正弦函数 Math.asin() 反正弦函数

Math.cos() 余弦函数 Math.acos() 反余弦函数

Math.tan() 正切函数 Math.atan() 反正切函数 Math.atan2()
商的反正切函数

Math.toDegrees() 弧度转化为角度
Math.toRadians() 角度转化为弧度
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: