java.lang.Math :
绝对值:
static int abs(int a)
static long abs(long a)
static float abs(float a)
static double abs(double a)
极值:
static int max(int a, int b)
static long max(long a, long b)
static float max(float a, float b)
static double max(double a, double b)
static int min(int a, int b)
static long min(long a, long b)
static float min(float a, float b)
static double min(double a, double b)
三角:
static double sin(double a) //正弦函数
static double sinh(double x) //双曲正弦函数
static double cos(double a) //余弦函数
static double cosh(double x) //双曲余弦函数
static double tan(double a) //正切函数
static double tanh(double x) //双曲正切函数
static double asin(double a)
static double acos(double a)
static double atan(double a)
static double atan2(double y, double x)
对数,指数:
static double log(double a)
static double log10(double a)
static double log1p(double x) // ln (x+1)
static double exp(double a)
static double expm1(double x) // e^a - 1
幂,根:
static double pow(double a, double b)
static double sqrt(double a)
static double cbrt(double a) // cube root
static double hypot(double x, double y) // sqrt(x2 +y2)
static double scalb(double d, int scaleFactor) // d × 2
scaleFactor
static float scalb(float f, int scaleFactor) // f × 2
scaleFactor
随机:
static double random()
最接近的整数:
static long round(double a)
static int round(float a)
浮点数:
static double nextAfter(double start, double direction)
static float nextAfter(float start, double direction)
static double nextDown(double d)
static float nextDown(float f)
static double nextUp(double d)
static float nextUp(float f)
static double ceil(double a)
static double floor(double a)
static int floorDiv(int x, int y)
static long floorDiv(long x, long y)
static int floorMod(int x, int y)
static long floorMod(long x, long y)
无溢出计算:如果溢出则抛异常
static int toIntExact(long value)
static int addExact(int x, int y)
static long addExact(long x, long y)
static int subtractExact(int x, int y)
static long subtractExact(long x, long y)
static int multiplyExact(int x, int y)
static long multiplyExact(long x, long y)
static int negateExact(int a)
static long negateExact(long a)
static int incrementExact(int a)
static long incrementExact(long a)
static int decrementExact(int a)
static long decrementExact(long a)
符号:
static double signum(double d)
static float signum(float f)
角度转换:
static double toDegrees(double angrad)
static double toRadians(double angdeg)
其他:
static int getExponent(double d)
static int getExponent(float f)
static double rint(double a) // round int
static double copySign(double magnitude, double sign) //采用 magnitude 的值和 sign 的符号
static float copySign(float magnitude, float sign)
static double IEEEremainder(double f1, double f2) // f1 除以 f2的余数
static double ulp(double d)
static float ulp(float f)
package com.hone.test; /**
* 测试java.lang.Math的方法
* @author Xia
*
*/
public class Maths { public static void main(String[] args) {
double d = 123.456;
double d2 = -123.456; int i = 123;
int b = 389;
System.out.printf("%.2f%n", d2); //按照格式输出2位小数
System.out.printf("%.3f%n", Math.abs(d2)); //Math.abs(d2) 获取绝对值 //------------没有溢出计算,如果有溢出则抛出异常--------------------
System.out.println(Math.addExact(i, b));
System.out.println(Math.incrementExact(i)); //如果溢出则加1 ------124 //------------极值--------------------
System.out.println(Math.max(d, d2)); //选择更大的一个值 123.456
System.out.println(Math.min(i, b)); //选择更小的一个值 123 //------------对数,指数--------------------
System.out.println(Math.log(d)); //输出以e为底的对数
System.out.println(Math.log10(100.0)); //输出以10为底的对数
System.out.println(Math.log10(100.0)); //输出以10为底的对数 System.out.println(Math.exp(1)); //输出以e为底的指数 //------------幂,根:--------------------
System.out.println(Math.pow(d, d2)); //表示d^d2
System.out.println(Math.sqrt(d)); //取d的均方根
System.out.println(Math.cbrt(1000)); //立方根 //------------随机数--------------------
System.out.println(Math.random()); //取(0,1)之间的随机数 //------------最接近的整数:--------------------
System.out.println(Math.round(d)); //四舍五入取整 System.out.println(Math.ceil(d)); //向上取整
System.out.println(Math.floor(d)); //向下取整
}
}