java中常见的math方法
java.lang.Math :
随机:
static double random() 返回一个介于[0,1)之间的随机数
拓展:
Math.ceil(Math.random()*10);返回的是1~10之间的随机整数
Math.floor(Math.random()*10);返回的是0~9之间的随机整数
Math.round(Math.random()*(y-x))+x; //返回x~y的随机整数
最接近的整数:
static long round(double a) 四舍五入
static int round(float a)
ceil()向上取整数
floor()向下取整数
绝对值:
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 × 2scaleFactor
static float scalb(float f, int scaleFactor) // f × 2scaleFactor
浮点数:
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)
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)); //向下取整
}
}