java中何时使用StrictMath的数学函数?

本文详细介绍了Java中的Math类和StrictMath类,包括平方根、幂运算、三角函数、对数、指数、极坐标转换及特殊数学函数。重点阐述了它们在解决数学问题时的应用与区别,特别是如何处理特殊值如NaN和正无穷大。通过实例演示了如何在Java中执行这些数学计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简单的介绍下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 e x - 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;

很浅显的使用,具体的实现算法,后来在研究。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值