Java數學運算
基本數字
通常使用內置數據類型,如:byte、short、int、long、float、double 。
public class HelloWorld {
public static void main(String[] args) {
byte a = 0x32;
short b = 50;
int c = 50;
long d = 50;
float e = 50.001F;
double f = 50.001D;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println("d = " + d);
System.out.println("e = " + e);
System.out.println("f = " + f);
}
}
數字對象Number
Java語言爲每一個內置數據類型提供了對應的包裝類。
所有的包裝類(Byte、Short、Integer、Long、Float、Double)都是抽象類Number的子類。
由編譯器特別支持的包裝稱爲裝箱,所以當內置數據類型被當作對象使用的時候,編譯器會把內置類型裝箱爲包裝類。相似的,編譯器也可以把一個對象拆箱爲內置類型。Number類屬於java.lang包。
public static void main(String[] args) {
Integer x = 5;
x = x + 10;
System.out.println(x);
}
當x被賦爲整型值時,由於x是一個對象,所以編譯器要對x進行裝箱。然後,爲了使x能進行加運算,所以要對x進行拆箱。
BigInteger/BigDecimal
BigInteger/BigDecimal的父類java.lang.Number 。Java Platform SE 8
public static void main(String[] args) {
// 這幾個測試,是爲了簡單超過int範圍內,Integer就不能再表示
System.out.println(Integer.MAX_VALUE);
// NumberFormatException
// Integer i = new Integer("2147483648");
// System.out.println(i);
// 通過大整數來創建對象
BigInteger bi = new BigInteger("2147483648");
System.out.println("bi:" + bi);
BigInteger bi1 = new BigInteger("100");
BigInteger bi2 = new BigInteger("50");
// public BigInteger add(BigInteger val):加
System.out.println("add:" + bi1.add(bi2));
// public BigInteger subtract(BigInteger val):加
System.out.println("subtract:" + bi1.subtract(bi2));
// public BigInteger multiply(BigInteger val):加
System.out.println("multiply:" + bi1.multiply(bi2));
// public BigInteger divide(BigInteger val):加
System.out.println("divide:" + bi1.divide(bi2));
// public BigInteger[] divideAndRemainder(BigInteger val):返回商和餘數的數組
BigInteger[] bis = bi1.divideAndRemainder(bi2);
System.out.println("商:" + bis[0]);
System.out.println("餘數:" + bis[1]);
}
public static void main(String[] args) {
BigDecimal bd1 = new BigDecimal("0.09");
BigDecimal bd2 = new BigDecimal("0.01");
System.out.println("add:" + bd1.add(bd2));
System.out.println("-------------------");
BigDecimal bd3 = new BigDecimal("1.0");
BigDecimal bd4 = new BigDecimal("0.32");
System.out.println("subtract:" + bd3.subtract(bd4));
System.out.println("-------------------");
BigDecimal bd5 = new BigDecimal("1.015");
BigDecimal bd6 = new BigDecimal("100");
System.out.println("multiply:" + bd5.multiply(bd6));
System.out.println("-------------------");
BigDecimal bd7 = new BigDecimal("1.301");
BigDecimal bd8 = new BigDecimal("100");
System.out.println("divide:" + bd7.divide(bd8));
System.out.println("divide:" + bd7.divide(bd8, 3, BigDecimal.ROUND_HALF_UP));
System.out.println("divide:" + bd7.divide(bd8, 8, BigDecimal.ROUND_HALF_UP));
}
Java Math類
Java 的 Math 包含了用於執行基本數學運算的屬性和方法,如初等指數、對數、平方根和三角函數。
Math 的方法都被定義爲 static 形式,通過 Math 類可以在主函數中直接調用。Java Platform SE 8
Modifier and Type | Method | Description |
---|---|---|
static double | abs(double a) | Returns the absolute value of a double value. |
static float | abs(float a) | Returns the absolute value of a float value. |
static int | abs(int a) | Returns the absolute value of an int value. |
static long | abs(long a) | Returns the absolute value of a long value. |
static double | acos(double a) | Returns the arc cosine of a value; the returned angle is in the range 0.0 through pi. |
static int | addExact(int x, int y) | Returns the sum of its arguments, throwing an exception if the result overflows an int. |
static long | addExact(long x, long y) | Returns the sum of its arguments, throwing an exception if the result overflows a long. |
static double | asin(double a) | Returns the arc sine of a value; the returned angle is in the range -pi/2 through pi/2. |
static double | atan(double a) | Returns the arc tangent of a value; the returned angle is in the range -pi/2 through pi/2. |
static double | atan2(double y, double x) | Returns the angle theta from the conversion of rectangular coordinates (x, y) to polar coordinates (r, theta). |
static double | cbrt(double a) | Returns the cube root of a double value. |
static double | ceil(double a) | Returns the smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer. |
static double | copySign(double magnitude, double sign) | Returns the first floating-point argument with the sign of the second floating-point argument. |
static float | copySign(float magnitude, float sign) | Returns the first floating-point argument with the sign of the second floating-point argument. |
static double | cos(double a) | Returns the trigonometric cosine of an angle. |
static double | cosh(double x) | Returns the hyperbolic cosine of a double value. |
static int | decrementExact(int a) | Returns the argument decremented by one, throwing an exception if the result overflows an int. |
static long | decrementExact(long a) | Returns the argument decremented by one, throwing an exception if the result overflows a long. |
static double | exp(double a) | Returns Euler’s number e raised to the power of a double value. |
static double | expm1(double x) | Returns ex -1. |
static double | floor(double a) | Returns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer. |
static int | floorDiv(int x, int y) | Returns the largest (closest to positive infinity) int value that is less than or equal to the algebraic quotient. |
static long | floorDiv(long x, long y) | Returns the largest (closest to positive infinity) long value that is less than or equal to the algebraic quotient. |
static int | floorMod(int x, int y) | Returns the floor modulus of the int arguments. |
static long | floorMod(long x, long y) | Returns the floor modulus of the long arguments. |
static int | getExponent(double d) | Returns the unbiased exponent used in the representation of a double. |
static int | getExponent(float f) | Returns the unbiased exponent used in the representation of a float. |
static double | hypot(double x, double y) | Returns sqrt(x2 +y2) without intermediate overflow or underflow. |
static double | IEEEremainder(double f1, double f2) | Computes the remainder operation on two arguments as prescribed by the IEEE 754 standard. |
static int | incrementExact(int a) | Returns the argument incremented by one, throwing an exception if the result overflows an int. |
static long | incrementExact(long a) | Returns the argument incremented by one, throwing an exception if the result overflows a long. |
static double | log(double a) | Returns the natural logarithm (base e) of a double value. |
static double | log10(double a) | Returns the base 10 logarithm of a double value. |
static double | log1p(double x) | Returns the natural logarithm of the sum of the argument and 1. |
static double | max(double a, double b) | Returns the greater of two double values. |
static float | max(float a, float b) | Returns the greater of two float values. |
static int | max(int a, int b) | Returns the greater of two int values. |
static long | max(long a, long b) | Returns the greater of two long values. |
static double | min(double a, double b) | Returns the smaller of two double values. |
static float | min(float a, float b) | Returns the smaller of two float values. |
static int | min(int a, int b) | Returns the smaller of two int values. |
static long | min(long a, long b) | Returns the smaller of two long values. |
static int | multiplyExact(int x, int y) | Returns the product of the arguments, throwing an exception if the result overflows an int. |
static long | multiplyExact(long x, long y) | Returns the product of the arguments, throwing an exception if the result overflows a long. |
static int | negateExact(int a) | Returns the negation of the argument, throwing an exception if the result overflows an int. |
static long | negateExact(long a) | Returns the negation of the argument, throwing an exception if the result overflows a long. |
static double | nextAfter(double start, double direction) | Returns the floating-point number adjacent to the first argument in the direction of the second argument. |
static float | nextAfter(float start, double direction) | Returns the floating-point number adjacent to the first argument in the direction of the second argument. |
static double | nextDown(double d) | Returns the floating-point value adjacent to d in the direction of negative infinity. |
static float | nextDown(float f) | Returns the floating-point value adjacent to f in the direction of negative infinity. |
static double | nextUp(double d) | Returns the floating-point value adjacent to d in the direction of positive infinity. |
static float | nextUp(float f) | Returns the floating-point value adjacent to f in the direction of positive infinity. |
static double | pow(double a, double b) | Returns the value of the first argument raised to the power of the second argument. |
static double | random() | Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. |
static double | rint(double a) | Returns the double value that is closest in value to the argument and is equal to a mathematical integer. |
static long | round(double a) | Returns the closest long to the argument, with ties rounding to positive infinity. |
static int | round(float a) | Returns the closest int to the argument, with ties rounding to positive infinity. |
static double | scalb(double d, int scaleFactor) | Returns d × 2scaleFactor rounded as if performed by a single correctly rounded floating-point multiply to a member of the double value set. |
static float | scalb(float f, int scaleFactor) | Returns f × 2scaleFactor rounded as if performed by a single correctly rounded floating-point multiply to a member of the float value set. |
static double | signum(double d) | Returns the signum function of the argument; zero if the argument is zero, 1.0 if the argument is greater than zero, -1.0 if the argument is less than zero. |
static float | signum(float f) | Returns the signum function of the argument; zero if the argument is zero, 1.0f if the argument is greater than zero, -1.0f if the argument is less than zero. |
static double | sin(double a) | Returns the trigonometric sine of an angle. |
static double | sinh(double x) | Returns the hyperbolic sine of a double value. |
static double | sqrt(double a) | Returns the correctly rounded positive square root of a double value. |
static int | subtractExact(int x, int y) | Returns the difference of the arguments, throwing an exception if the result overflows an int. |
static long | subtractExact(long x, long y) | Returns the difference of the arguments, throwing an exception if the result overflows a long. |
static double | tan(double a) | Returns the trigonometric tangent of an angle. |
static double | tanh(double x) | Returns the hyperbolic tangent of a double value. |
static double | toDegrees(double angrad) | Converts an angle measured in radians to an approximately equivalent angle measured in degrees. |
static int | toIntExact(long value) | Returns the value of the long argument; throwing an exception if the value overflows an int. |
static double | toRadians(double angdeg) | Converts an angle measured in degrees to an approximately equivalent angle measured in radians. |
static double | ulp(double d) | Returns the size of an ulp of the argument. |
static float | ulp(float f) | Returns the size of an ulp of the argument. |
public static void main(String[] args) {
// public static final double PI
System.out.println("PI:" + Math.PI);
System.out.println("90 度的正弦值:" + Math.sin(Math.PI / 2));
System.out.println("0度的餘弦值:" + Math.cos(0));
System.out.println("60度的正切值:" + Math.tan(Math.PI / 3));
System.out.println("1的反正切值: " + Math.atan(1));
System.out.println("π/2的角度值:" + Math.toDegrees(Math.PI / 2));
System.out.println("--------------");
// public static final double E
System.out.println("E:" + Math.E);
System.out.println("--------------");
// public static int abs(int a):絕對值
System.out.println("abs:" + Math.abs(10));
System.out.println("abs:" + Math.abs(-10));
System.out.println("--------------");
// public static int max(int a,int b):最大值
System.out.println("max:" + Math.max(12, 23));
// 需求:我要獲取三個數據中的最大值
// 方法的嵌套調用
System.out.println("max:" + Math.max(Math.max(12, 23), 18));
// 需求:我要獲取四個數據中的最大值
System.out.println("max:" + Math.max(Math.max(12, 78), Math.max(34, 56)));
System.out.println("--------------");
// public static double pow(double a,double b):a的b次冪
System.out.println("pow:" + Math.pow(2, 3));
System.out.println("--------------");
// public static double random():隨機數 [0.0,1.0)
System.out.println("random:" + Math.random());
// 獲取一個1-100之間的隨機數
System.out.println("random:" + ((int) (Math.random() * 100) + 1));
System.out.println("--------------");
// public static double sqrt(double a):正平方根
System.out.println("sqrt:" + Math.sqrt(4));
System.out.println("--------------");
// public static int round(float a) 四捨五入(參數爲double的自學)
System.out.println("round:" + Math.round(12.34f));
System.out.println("round:" + Math.round(12.56f));
System.out.println("--------------");
// public static double ceil(double a):向上取整
System.out.println("ceil:" + Math.ceil(12.34));
System.out.println("ceil:" + Math.ceil(12.56));
System.out.println("--------------");
// public static double floor(double a):向下取整
System.out.println("floor:" + Math.floor(12.34));
System.out.println("floor:" + Math.floor(12.56));
}