java中何時使用StrictMath的數學函數?

簡單的介紹下Math類和StrictMath類,一般的數學計算中,需要調用已經封裝好的Math類;

1、比如,我們時常用到的平方根函數,數學函數類的官方說明;

double java.lang.Math.sqrt(double a)

Returns the correctly rounded positive square root of a double value. Special cases:

  • If the argument is NaN or less than zero, then the result is NaN.
  • If the argument is positive infinity, then the result is positive infinity.
  • If the argument is positive zero or negative zero, then the result is the same as the argument.
Otherwise, the result is the double value closest to the true mathematical square root of the argument value.
Parameters:
a a value.
Returns:
the positive square root of a. If the argument is NaN or less than zero, the result is NaN.
分析;Math類返回的是double類型的值

		double double_x = 8;
		double double_y = Math.sqrt(double_x);
		System.out.println(double_y);
打印;2.8284271247461903

		double double_x = -4;
		double double_y = Math.sqrt(double_x);
		System.out.println(double_y);
參數是負數,則返回“不是數值NaN”,打印;NaN

同樣,如果參數是正無窮大,則返回正無窮大,如果參數是0(不論正負),則返回0.0,顯然,Math類方法都是靜態方法,不需要使用對象即可調用!


2、常用的冪運算,java中沒有冪運算,必須藉助方法pow來實現;

		double x = Math.pow(3, 2);
		System.out.println(x);
打印;9.0

double java.lang.Math.pow(double a, double b);參數和返回值都是double類型,結果爲a的b次方。


3、還有常用的一些三角函數

		// 仍然都是返回double,參數也是double類型
		System.out.println(Math.sin(1.5707963267948966));// 打印1.0
		System.out.println(Math.cos(0));// 打印1.0
		System.out.println(Math.tan(0));// 打印0.0
		// cos(0)=1;則反函數acos打印0.0
		System.out.println(Math.acos(1));
		System.out.println(Math.asin(1));//1.5707963267948966
		System.out.println(Math.atan(0));//0.0


比較特殊的一個;double java.lang.Math.atan2(double y, double x);

Returns the angle theta from the conversion of rectangular coordinates (xy) to polar coordinates (r, theta). This method computes the phasetheta by computing an arc tangent ofy/x in the range of -pi topi

ps;

極座標,r是極徑,theta也即是賊他角,有公式;

x=r*cos(theta);

y=r*sin(theta);

x^2 + y^2 = r^2;

Parameters:

y the ordinate coordinate

x the abscissa coordinate

Returns:the theta component of the point (rtheta) in polar coordinates that corresponds to the point (xy) in Cartesian coordinates.

參數是極座標的參數,但是返回的值是在對應的(相符合)直角座標系的範圍內計算得出的(double類型的),反正切返回的角等於 X 軸正方向與通過原點和給定座標點 (Y座標, X座標) 的射線之間的夾角(y/x)的結果,以弧度表示,並介於 -pi 到 pi 之間。

4、對數函數

		System.err.println(Math.E);// 2.718281828459045
		System.out.println(Math.PI);// 3.141592653589793
		System.out.println(Math.log(Math.E));// 1.0 這是自然對數(以e爲底的e的對數爲1)
		System.out.println(Math.log10(10.0));// 1.0 這是以10爲底的10.0的對數,爲1

特別的;double java.lang.Math.log1p(double x)

Returns the natural logarithm of the sum of the argument and 1. Note that for small valuesx, the result oflog1p(x) is much closer to the true result of ln(1 +x) than the floating-point evaluation oflog(1.0+x)

The computed result must be within 1 ulp of the exact result. Results must be semi-monotonic.

Parameters:
x a value
Returns:
the value ln(x + 1), the natural log of x + 1
Since:

1.5

返回的是ln(x+1)的結果(自然對數)


5、指數函數(e爲底的)

PS;爲了避免使用前綴(Math),我們可以採用靜態導入

import static java.lang.Math.*;
這樣的話,就不必每次都寫Math.了。

System.out.println(exp(1.0));
打印;2.718281828459045

double java.lang.Math.expm1(double x)

Returns ex -1. Note that for values of x near 0, the exact sum ofexpm1(x) + 1 is much closer to the true result ofex thanexp(x).

Parameters:x the exponent to raise e to in the computation ofex -1.

Returns:the value ex - 1.
Since:1.5
System.out.println(expm1(1.0));
打印;1.718281828459045

Returns:the value ex - 1.


必須知道,這個Math類是使用的計算機的浮點運算,來達到最佳的計算性能,不過結果有時候會出現不可預測的情況

如果想得一個完全的可以預測的結果的話,就必須使用StrictMath類,它的實現算法是來自免費發佈的數學庫(fdlibm),全部使用c語言編寫


作用;實現java程序的輕量級應用,使程序計算在所有的平臺確定都得到一樣的結果。

		System.out.println(StrictMath.PI);// 3.141592653589793
		System.out.println(StrictMath.abs(-1.0));// 1.0
java.lang.StrictMath

The class StrictMath contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions.To help ensure portability of Java programs, the definitions of some of the numeric functions in this package require that they produce the same results as certain published algorithms. These algorithms are available from the well-known network librarynetlib as the package "Freely Distributable Math Library," fdlibm. These algorithms, which are written in the C programming language, are then to be understood as executed with all floating-point operations following the rules of Java floating-point arithmetic.

The Java math library is defined with respect to fdlibm version 5.3. Wherefdlibm provides more than one definition for a function (such as acos), use the "IEEE 754 core function" version (residing in a file whose name begins with the lettere). The methods which require fdlibm semantics are sin, cos, tan, asin, acos,atan, exp, log, log10, cbrt, atan2, pow, sinh, cosh,tanh, hypot, expm1, and log1p.

Since:
1.3
Author:
unascribed
Joseph D. Darcy
包含;基本的指數,對數,平方跟,三角函數等


ps;

很淺顯的使用,具體的實現算法,後來在研究。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章