Java編程語言支持基本算術及其算術運算符:+
、-
、*
、/
和%
,java.lang
包中的Math類提供了用於執行更高級數學計算的方法和常量。html
Math類中的方法都是靜態的,所以你能夠直接從類中調用它們,以下所示:java
Math.cos(angle);
使用靜態導入語言功能,你沒必要在每一個數學函數前面寫Math
:git
import static java.lang.Math.*;
這容許你經過簡單名稱調用Math
類方法,例如:github
cos(angle);
Math
類包含兩個常量:編程
Math.E
,是天然對數的基數。Math.PI
,這是圓周率。Math
類還包含40多種靜態方法,下表列出了許多基本方法。segmentfault
方法 | 描述 |
---|---|
double abs(double d) float abs(float f) int abs(int i) long abs(long lng) |
返回參數的絕對值。 |
double ceil(double d) |
返回大於或等於參數的最小整數,做爲double 返回。 |
double floor(double d) |
返回小於或等於參數的最大整數,做爲double 返回。 |
double rint(double d) |
返回與參數值最接近的整數,做爲double 返回。 |
long round(double d) int round(float f) |
根據方法的返回類型,返回與參數最近的long 或int 。 |
double min(double arg1, double arg2) float min(float arg1, float arg2) int min(int arg1, int arg2) long min(long arg1, long arg2) |
返回兩個參數中較小的一個。 |
double max(double arg1, double arg2) float max(float arg1, float arg2) int max(int arg1, int arg2) long max(long arg1, long arg2) |
返回兩個參數中較大的一個。 |
如下程序BasicMathDemo說明了如何使用其中一些方法:api
public class BasicMathDemo { public static void main(String[] args) { double a = -191.635; double b = 43.74; int c = 16, d = 45; System.out.printf("The absolute value " + "of %.3f is %.3f%n", a, Math.abs(a)); System.out.printf("The ceiling of " + "%.2f is %.0f%n", b, Math.ceil(b)); System.out.printf("The floor of " + "%.2f is %.0f%n", b, Math.floor(b)); System.out.printf("The rint of %.2f " + "is %.0f%n", b, Math.rint(b)); System.out.printf("The max of %d and " + "%d is %d%n", c, d, Math.max(c, d)); System.out.printf("The min of of %d " + "and %d is %d%n", c, d, Math.min(c, d)); } }
這是該程序的輸出:oracle
The absolute value of -191.635 is 191.635 The ceiling of 43.74 is 44 The floor of 43.74 is 43 The rint of 43.74 is 44 The max of 16 and 45 is 45 The min of 16 and 45 is 16
下表列出了Math
類的指數和對數方法。dom
方法 | 描述 |
---|---|
double exp(double d) |
返回天然對數的基數e 的參數次方。 |
double log(double d) |
返回參數的天然對數。 |
double pow(double base, double exponent) |
返回第一個參數的值提高到第二個參數的次冪。 |
double sqrt(double d) |
返回參數的平方根。 |
如下程序ExponentialDemo顯示e
的值,而後對任意選擇的數字調用上表中列出的每一個方法:編程語言
public class ExponentialDemo { public static void main(String[] args) { double x = 11.635; double y = 2.76; System.out.printf("The value of " + "e is %.4f%n", Math.E); System.out.printf("exp(%.3f) " + "is %.3f%n", x, Math.exp(x)); System.out.printf("log(%.3f) is " + "%.3f%n", x, Math.log(x)); System.out.printf("pow(%.3f, %.3f) " + "is %.3f%n", x, y, Math.pow(x, y)); System.out.printf("sqrt(%.3f) is " + "%.3f%n", x, Math.sqrt(x)); } }
這是運行ExponentialDemo
時你將看到的輸出:
The value of e is 2.7183 exp(11.635) is 112983.831 log(11.635) is 2.454 pow(11.635, 2.760) is 874.008 sqrt(11.635) is 3.411
Math
類還提供了三角函數功能的集合,以下表所示,傳遞給每一個方法的值是以弧度表示的角度,你可使用toRadians
方法將度數轉換爲弧度。
方法 | 描述 |
---|---|
double sin(double d) |
返回指定double 值的正弦值。 |
double cos(double d) |
返回指定double 值的餘弦值。 |
double tan(double d) |
返回指定double 值的正切值。 |
double asin(double d) |
返回指定double 值的反正弦值。 |
double acos(double d) |
返回指定double 值的反餘弦值。 |
double atan(double d) |
返回指定double 值的反正切值。 |
double atan2(double y, double x) |
將直角座標(x,y)轉換爲極座標(r,theta)並返回theta。 |
double toDegrees(double d) double toDegrees(double d) |
將參數轉換爲度數或弧度。 |
這是一個程序TrigonometricDemo,它使用這些方法中的每個來計算45度角的各類三角函數值:
public class TrigonometricDemo { public static void main(String[] args) { double degrees = 45.0; double radians = Math.toRadians(degrees); System.out.format("The value of pi " + "is %.4f%n", Math.PI); System.out.format("The sine of %.1f " + "degrees is %.4f%n", degrees, Math.sin(radians)); System.out.format("The cosine of %.1f " + "degrees is %.4f%n", degrees, Math.cos(radians)); System.out.format("The tangent of %.1f " + "degrees is %.4f%n", degrees, Math.tan(radians)); System.out.format("The arcsine of %.4f " + "is %.4f degrees %n", Math.sin(radians), Math.toDegrees(Math.asin(Math.sin(radians)))); System.out.format("The arccosine of %.4f " + "is %.4f degrees %n", Math.cos(radians), Math.toDegrees(Math.acos(Math.cos(radians)))); System.out.format("The arctangent of %.4f " + "is %.4f degrees %n", Math.tan(radians), Math.toDegrees(Math.atan(Math.tan(radians)))); } }
該程序的輸出以下:
The value of pi is 3.1416 The sine of 45.0 degrees is 0.7071 The cosine of 45.0 degrees is 0.7071 The tangent of 45.0 degrees is 1.0000 The arcsine of 0.7071 is 45.0000 degrees The arccosine of 0.7071 is 45.0000 degrees The arctangent of 1.0000 is 45.0000 degrees
random()
方法返回一個介於0.0和1.0之間的僞隨機選擇的數字,範圍包括0.0但不包括1.0,換句話說:0.0 <= Math.random() < 1.0
。要獲取不一樣範圍內的數字,能夠對隨機方法返回的值執行算術運算,例如,要生成0到9之間的整數,你能夠編寫:
int number = (int)(Math.random() * 10);
經過將該值乘以10,可能值的範圍變爲0.0 <= number < 10.0
。
當你須要生成單個隨機數時,使用Math.random
能夠很好地工做,若是須要生成一系列隨機數,則應建立java.util.Random
實例並在該對象上調用方法以生成數字。