Java.lang.Math 类Java |设置 2
Java.lang.Math 类Java |设置 1
更多方法:
- cosh() : Java.lang.Math.cosh()方法返回传递参数的双曲余弦值。
特别案例 :- 如果参数为 NaN,则结果为 NaN。
- 如果参数为零,则结果为 1.0。
- 结果是 +ve 无穷大,如果参数是无限的。
句法:
public static double cosh(double arg) Parameters: arg - The number whose hyperbolic cosine is to be returned. Returns: the hyperbolic cosine of the argument arg.
- decrementExact() : Java.lang.Math.decrementExact()方法将传递参数的值减一。
句法:public static int decrementExact(int arg) or public static long decrementExact(long arg) Parameters: arg - argument passed. Returns: return argument decremented by one. Throws: Exception if the result overflows long or int datatype, according to the argumented data type.
- exp() : Java.lang.Math.exp(double arg)方法返回欧拉数提高到双参数的幂。
重要案例:- 如果参数为 NaN,则结果为 NaN。
- 如果参数是 +ve infinity,则结果是 +ve infinity。
- 结果是 +ve 零,如果参数是 -ve 无穷大。
句法:
public static double exp(double arg) Parameters: arg - argument passed. Returns: Euler’s number raised to the power of passed argument
Java代码解释 lang.Math 类中的 exp()、decrementExact()、cosh() 方法。
// Java program explaining lang.Math class methods // exp(), decrementExact(), cosh() import java.math.*; public class NewClass { public static void main(String[] args) { // Use of cosh() method double value = 2; double coshValue = Math.cosh(value); System.out.println("Hyperbolic Cosine of " + coshValue); System.out.println(""); // Use of decrementExact() method int result = Math.decrementExact(3051); System.out.println("Use of decrementExact() : " + result); System.out.println(""); // Use of exp() method // declare the exponent to be used double exponent = 34; // raise e to exponent declared double expVal = Math.exp(exponent); System.out.println("Value of exp : "+ expVal); } }
输出:
Using addExact() : 9 acos value of Asini : NaN acos value of Asinj : 0.054858647341251204 cube root : 6.0
- incrementExact() : Java.lang.Math.incrementExact()方法通过增加它的值来返回参数。
Syntax: public static int incrementExact(int arg) or public static long incrementExact(long arg) Parameters: arg - the argument Returns: incremented value of the argument
- log10() : Java.lang.Math.log10()方法返回传递参数的base10对数值。
Syntax: public static double log(double arg) Parameters: arg - argument passed. Returns: base10 logarithmic value of the argument passed.
- pow() : Java.lang.Math.pow(double b, double e)方法返回值为b e
Syntax: public static double pow(double b,double e) Parameters: b : base e : exponent Returns: value as baseexponent
解释 lang.Math 类中的 incrementExact()、log10()、pow() 方法的Java代码。
// Java program explaining lang.MATH class methods // incrementExact(), log10(), pow() import java.lang.*; public class NewClass { public static void main(String[] args) { // Use of incrementExact() method int f1 = 30, f2 = -56; f1 =Math.incrementExact(f1); System.out.println("Incremented value of f1 : "+f1); f2 =Math.incrementExact(f2); System.out.println("Incremented value of f2 : "+f2); System.out.println(""); // Use of log10() method double value = 10; double logValue = Math.log10(value); System.out.println("Log10 value of 10 : "+logValue); System.out.println(""); // Use of pow() method double b = 10, e = 2; double power = Math.pow(b,e); System.out.println("Use of pow() : "+power); } }
输出 :
Incremented value of f1 : 31 Incremented value of f2 : -55 Log10 value of 10 : 1.0 Use of pow() : 100.0
- signum() : Java.lang.Math.signum()方法返回传递的参数的符号值。
-1 if x < 0 signum fun(x) = 0 if x = 0 1 if x > 0
- 注意:结果为 NaN,如果传递参数为 NaN。
句法:
public static double signum(double x) or public static float signum(float x) Parameters: x - the argument whose signum value we need Returns: signum value of x
- round() : Java.lang.Math.round()方法将传递的参数四舍五入到最接近的小数位。
注意:如果参数为 NaN,则结果为 0。
句法:public static long round(long arg) or public static double round(double arg) Parameters: arg - argument needs to round off Returns: round off value of the argument
- max() : Java.lang.Math.max(double v1, double v2)方法返回两个传递的参数值中较大的值。
该方法仅使用幅度进行比较,不考虑任何符号。
句法:public static double max(double v1, double v2) Parameters: v1 - first value v2 - second value Returns: v1 or v2 based on which number is greater. It can return either of the two if v1 = v2.
Java代码解释 lang.Math 类中的 signum()、round()、max() 方法。
// Java code explaining the lang.Math Class methods // signum(), round(), max() import java.lang.*; public class NewClass { public static void main(String args[]) { // Use of signum() method double x = 10.4556, y = -23.34789; double signm = Math.signum(x); System.out.println("Signum of 10.45 = "+signm); signm = Math.signum(y); System.out.println("Signum of -23.34 = "+signm); System.out.println(""); // Use of round() method double r1 = Math.round(x); System.out.println("Round off 10.4556 = "+r1); double r2 = Math.round(y); System.out.println("Round off 23.34789 = "+r2); System.out.println(""); // Use of max() method on r1 and r2 double m = Math.max(r1, r2); System.out.println("Max b/w r1 and r2 = "+r2); } }
输出:
Signum of 10.45 = 1.0 Signum of -23.34 = -1.0 Round off 10.4556 = 10.0 Round off 23.34789 = -23.0 Max b/w r1 and r2 = -23.0
- log1p() : Java.lang.Math.log1p()方法返回(传递的参数+ 1)的自然对数。
句法:public static double log1p(double arg) Parameters: arg - the argument Returns: log of (argument + 1). This result is within 1 unit in the last place of exact result.
- ulp() : Java.lang.Math.ulp()方法返回最小精度单位(ulp),即。两个浮点数之间的最小距离。
在这里,它是参数和下一个较大值的最小距离 b/w。
句法:public static double ulp(double arg) or public static float ulp(float arg) Parameters: arg - argument passed. Returns: least distance b/w the argument and next larger value.
Java代码解释 lang.Math 类中的 ulp()、log1p() 方法。
// Java code explaining the lang.Math Class methods // ulp(), log1p() import java.lang.*; public class NewClass { public static void main(String args[]) { // Use of ulp() method double x = 34.652, y = -23.34789; double u = Math.ulp(x); System.out.println("ulp of 34.652 : "+u); u = Math.ulp(y); System.out.println("ulp of -23.34789 : "+u); System.out.println(""); // Use of log() method double l = 99; double l1 = Math.log1p(l); System.out.println("Log of (1 + 99) : "+l1); l1 = Math.log(100); System.out.println("Log of 100 : "+l1); } }
输出:
ulp of 34.652 : 7.105427357601002E-15 ulp of -23.34789 : 3.552713678800501E-15 Log of (1 + 99) : 4.605170185988092 Log of 100 : 4.605170185988092