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)
1 package com.hone.test; 2 3 /** 4 * 測試java.lang.Math的方法 5 * @author Xia 6 * 7 */ 8 public class Maths { 9 10 public static void main(String[] args) { 11 double d = 123.456; 12 double d2 = -123.456; 13 14 int i = 123; 15 int b = 389; 16 System.out.printf("%.2f%n", d2); //按照格式輸出2位小數 17 System.out.printf("%.3f%n", Math.abs(d2)); //Math.abs(d2) 獲取絕對值 18 19 //------------沒有溢出計算,如果有溢出則拋出異常-------------------- 20 System.out.println(Math.addExact(i, b)); 21 System.out.println(Math.incrementExact(i)); //如果溢出則加1 ------124 22 23 //------------極值-------------------- 24 System.out.println(Math.max(d, d2)); //選擇更大的一個值 123.456 25 System.out.println(Math.min(i, b)); //選擇更小的一個值 123 26 27 //------------對數,指數-------------------- 28 System.out.println(Math.log(d)); //輸出以e為底的對數 29 System.out.println(Math.log10(100.0)); //輸出以10為底的對數 30 System.out.println(Math.log10(100.0)); //輸出以10為底的對數 31 32 System.out.println(Math.exp(1)); //輸出以e為底的指數 33 34 //------------冪,根:-------------------- 35 System.out.println(Math.pow(d, d2)); //表示d^d2 36 System.out.println(Math.sqrt(d)); //取d的均方根 37 System.out.println(Math.cbrt(1000)); //立方根 38 39 //------------隨機數-------------------- 40 System.out.println(Math.random()); //取(0,1)之間的隨機數 41 42 //------------最接近的整數:-------------------- 43 System.out.println(Math.round(d)); //四舍五入取整 44 45 System.out.println(Math.ceil(d)); //向上取整 46 System.out.println(Math.floor(d)); //向下取整 47 } 48 }