Math类
Math包含很多方法,适用于执行基本数字运算的,比如指数,对数,四舍五入等等。。。
碰到如需数字计算的,我们应该首先想到Math类。
方法讲解
举例:
System.out.println(Math.abs(-100));//取绝对值
System.out.println(Math.min(-100,200));//取最小值
System.out.println(Math.manx(-100,200));//取最大值
System.out.println(Math.round(100.5));//四舍五入
System.out.println(Math.floor(1.5));//返回小于等于参数的最大整数
System.out.println(Math.ceil(1.5));//返回大于等于参数的最下整数
舍是变小,入是变大:-100.5四舍五入后为-100;
Math方法类一览:
变量和类型 | 方法 | 描述 |
---|---|---|
static double | abs(double a) | 返回 double 值的绝对值。 |
static float | abs(float a) | 返回 float 值的绝对值。 |
static int | abs(int a) | 返回 int 值的绝对值。 |
static long | abs(long a) | 返回 long 值的绝对值。 |
static double | acos(double a) | 返回值的反余弦值; 返回的角度在0.0到pi的范围内。 |
static int | addExact(int x, int y) | 返回其参数的总和,如果结果溢出 int 则抛出异常。 |
static long | addExact(long x, long y) | 返回其参数的总和,如果结果溢出 long 则抛出异常。 |
static double | asin(double a) | 返回值的反正弦值; 返回的角度在*-pi* / 2到pi / 2的范围内。 |
static double | atan(double a) | 返回值的反正切值; 返回的角度在*-pi* / 2到pi / 2的范围内。 |
static double | atan2(double y, double x) | 返回从直角坐标(转换角度 theta x , y )为极坐标 (R,θ-)。 |
static double | cbrt(double a) | 返回 double 值的多维数据集根。 |
static double | ceil(double a) | 返回大于或等于参数且等于数学整数的最小值(最接近负无穷大) double 。 |
static double | copySign(double magnitude, double sign) | 返回带有第二个浮点参数符号的第一个浮点参数。 |
static float | copySign(float magnitude, float sign) | 返回带有第二个浮点参数符号的第一个浮点参数。 |
static double | cos(double a) | 返回角度的三角余弦值。 |
static double | cosh(double x) | 返回 double 值的双曲余弦值。 |
static int | decrementExact(int a) | 返回参数递减1,如果结果溢出 int 则抛出异常。 |
static long | decrementExact(long a) | 返回参数递减1,如果结果溢出 long 则抛出异常。 |
static double | exp(double a) | 返回Euler的数字 e ,其值 double 值。 |
static double | expm1(double x) | 返回 e x -1。 |
static double | floor(double a) | 返回小于或等于参数且等于数学整数的最大值(最接近正无穷大) double 。 |
static int | floorDiv(int x, int y) | 返回小于或等于代数商的最大值(最接近正无穷大) int 。 |
static long | floorDiv(long x, int y) | 返回小于或等于代数商的最大值(最接近正无穷大) long 。 |
static long | floorDiv(long x, long y) | 返回小于或等于代数商的最大值(最接近正无穷大) long 。 |
static int | floorMod(int x, int y) | 返回 int 参数的floor模数。 |
static int | floorMod(long x, int y) | 返回 long 和 int 参数的floor数。 |
static long | floorMod(long x, long y) | 返回 long 参数的floor模数。 |
static double | fma(double a, double b, double c) | 返回三个参数的融合乘法加法; 也就是说,返回与第三个参数相加的前两个参数的精确乘积,然后舍入一次到最接近的double 。 |
static float | fma(float a, float b, float c) | 返回三个参数的融合乘法加法; 也就是说,返回与第三个参数相加的前两个参数的精确乘积,然后舍入一次到最接近的float 。 |
static int | getExponent(double d) | 返回 double 表示中使用的无偏指数。 |
static int | getExponent(float f) | 返回 float 表示中使用的无偏指数。 |
static double | hypot(double x, double y) | 返回sqrt( x 2 + y 2 ),没有中间溢出或下溢。 |
static double | IEEEremainder(double f1, double f2) | 根据IEEE 754标准规定,计算两个参数的余数运算。 |
static int | incrementExact(int a) | 返回以1递增的参数,如果结果溢出 int 则抛出异常。 |
static long | incrementExact(long a) | 返回以1递增的参数,如果结果溢出 long 则抛出异常。 |
static double | log(double a) | 返回 double 值的自然对数(基数 e )。 |
static double | log10(double a) | 返回 double 值的基数10对数。 |
static double | log1p(double x) | 返回参数和的总和的自然对数。 |
static double | max(double a, double b) | 返回两个 double 值中较大的 double 。 |
static float | max(float a, float b) | 返回两个 float 值中较大的 float 。 |
static int | max(int a, int b) | 返回两个 int 值中较大的 int 。 |
static long | max(long a, long b) | 返回两个 long 值中较大的 long 。 |
static double | min(double a, double b) | 返回两个 double 值中较小的 double 。 |
static float | min(float a, float b) | 返回两个 float 值中较小的 float 。 |
static int | min(int a, int b) | 返回两个 int 值中较小的 int 。 |
static long | min(long a, long b) | 返回两个 long 值中较小的 long 。 |
static int | multiplyExact(int x, int y) | 返回参数的乘积,如果结果溢出 int 则抛出异常。 |
static long | multiplyExact(long x, int y) | 返回参数的乘积,如果结果溢出 long 则抛出异常。 |
static long | multiplyExact(long x, long y) | 返回参数的乘积,如果结果溢出 long 则抛出异常。 |
static long | multiplyFull(int x, int y) | 返回参数的确切数学乘积。 |
static long | multiplyHigh(long x, long y) | 返回 long 作为两个64位因子的128位乘积的最高64位。 |
static int | negateExact(int a) | 返回参数的否定,如果结果溢出 int 则抛出异常。 |
static long | negateExact(long a) | 返回参数的否定,如果结果溢出 long 则抛出异常。 |
static double | nextAfter(double start, double direction) | 返回第二个参数方向上第一个参数旁边的浮点数。 |
static float | nextAfter(float start, double direction) | 返回第二个参数方向上第一个参数旁边的浮点数。 |
static double | nextDown(double d) | 返回负无穷大方向上与 d 相邻的浮点值。 |
static float | nextDown(float f) | 返回负无穷大方向上与 f 相邻的浮点值。 |
static double | nextUp(double d) | 返回正无穷大方向上与 d 相邻的浮点值。 |
static float | nextUp(float f) | 返回正无穷大方向上与 f 相邻的浮点值。 |
static double | pow(double a, double b) | 返回第一个参数的值,该值是第二个参数的幂。 |
static double | random() | 返回带有正号的 double 值,大于或等于 0.0 且小于 1.0 。 |
static double | rint(double a) | 返回与 double 值最接近的 double 值,该值等于数学整数。 |
static long | round(double a) | 返回与参数最接近的 long ,并将关系四舍五入为正无穷大。 |
static int | round(float a) | 返回与参数最接近的 int ,并将关系四舍五入为正无穷大。 |
static double | scalb(double d, int scaleFactor) | 返回 d ×2 scaleFactor 舍入,就像通过单个正确舍入的浮点乘以双 scaleFactor 值集的成员一样。 |
static float | scalb(float f, int scaleFactor) | 返回 f ×2 scaleFactor 舍入,就像通过单个正确舍入的浮点乘以浮点值集的成员一样。 |
static double | signum(double d) | 返回参数的signum函数; 如果参数为零,则为零;如果参数大于零,则为1.0;如果参数小于零,则为-1.0。 |
static float | signum(float f) | 返回参数的signum函数; 如果参数为零则为零,如果参数大于零则为1.0f,如果参数小于零则为-1.0f。 |
static double | sin(double a) | 返回角度的三角正弦值。 |
static double | sinh(double x) | 返回 double 值的双曲正弦值。 |
static double | sqrt(double a) | 返回 double 值的正确舍入正平方根。 |
static int | subtractExact(int x, int y) | 返回参数的差异,如果结果溢出 int 则抛出异常。 |
static long | subtractExact(long x, long y) | 返回参数的差异,如果结果溢出 long 则抛出异常。 |
static double | tan(double a) | 返回角度的三角正切。 |
static double | tanh(double x) | 返回 double 值的双曲正切值。 |
static double | toDegrees(double angrad) | 将以弧度测量的角度转换为以度为单位测量的近似等效角度。 |
static int | toIntExact(long value) | 返回long 参数的值; 如果值溢出int 则抛出异常。 |
static double | toRadians(double angdeg) | 将以度为单位测量的角度转换为以弧度为单位测量的近似等效角度。 |
static double | ulp(double d) | 返回参数的ulp大小。 |
static float | ulp(float f) | 返回参数的ulp大小。 |