📜  Java.lang.Math 类Java |设置 1

📅  最后修改于: 2022-05-13 01:54:48.782000             🧑  作者: Mango

Java.lang.Math 类Java |设置 1

数学类方法有助于执行数值运算,如平方、平方根、立方、立方根、指数和三角运算

宣言 :

public final class Math
   extends Object

什么是 NaN 参数?
一个保持双精度类型的非数字 (NaN) 值的常量。它相当于 Double.longBitsToDouble(0x7ff8000000000000L) 返回的值。

lang.math 类的方法:

1. abs() : Java.lang.Math.abs()方法返回传递的任何类型参数的绝对值。此方法可以处理所有数据类型。

  • 如果参数为正零或负零,则结果为正零。
  • 如果参数是无限的,则结果是正无穷大。
  • 结果是 NaN,如果传递的参数是 NaN。

句法:

public static datatype abs(datatype arg)
Parameters:
arg - the argument whose absolute value we need
Returns:
absolute value of the passed argument.

2. acos() : Java.lang.Math.acos()方法返回传入参数的反余弦值。
反余弦是传递的参数的反余弦。
acos(arg) = cos -1 of arg

特例:如果参数为 NaN 或其绝对值大于 1,则结果为 NaN。
句法:

public static double acos(double a)
Parameters:
a - the argument whose arc cosine value we need.
    argument is taken as radian    
Returns:
arc cosine value of the argument.

3. toRadians() : Java.lang.Math.toRadians(double deg)方法将参数(度)转换为弧度。
注意:数学类通常将弧度作为输入,这在实际应用中非常不同,因为角度通常以度数表示。

句法:

public static double toRadians(double deg)
Parameters:
deg - degree angle needs to be in radian.
Returns:
radians equivalent of the degree-argument passed.

Java代码解释 lang.Math 类中的 abs()、acos()、toRadians() 方法。

Java
// Java program explaining lang.Math class methods
// abs(), acos(), toRadians()
 
import java.lang.*;
public class NewClass
{
    public static void main(String[] args)
    {
        // Declaring the variables
        int Vali = -1;
        float Valf = .5f;
 
        // Printing the values
        System.out.println("Initial value of int  : "+Vali);
        System.out.println("Initial value of int  : "+Valf);
 
 
        // Use of .abs() method to get the absoluteValue
        int Absi = Math.abs(Vali);
        float Absf = Math.abs(Valf);
 
        System.out.println("Absolute value of int : "+Absi);
        System.out.println("Absolute value of int : "+Absf);
        System.out.println("");
 
        // Use of acos() method
        // Value greater than 1, so passing NaN
        double Acosi = Math.acos(60);
        System.out.println("acos value of Acosi : "+Acosi);
        double x = Math.PI;
 
        // Use of toRadian() method
        x = Math.toRadians(x);
        double Acosj = Math.acos(x);
        System.out.println("acos value of Acosj : "+Acosj);
         
    }
}


Java
// Java program explaining lang.Math class methods
// asin(), cbrt()
 
import java.lang.*;
public class NewClass
{
 
    public static void main(String[] args)
    {
        int a = 1, b = 8;
        int radd = a+b;
 
        // Use of asin() method
        // Value greater than 1, so passing NaN
        double Asini = Math.asin(radd);
        System.out.println("asin value of Asini : " + Asini);
        double x = Math.PI;
 
        // Use of toRadian() method
        x = Math.toRadians(x);
        double Asinj = Math.asin(x);
        System.out.println("asin value of Asinj : " + Asinj);
        System.out.println("");
 
        // Use of cbrt() method
        double cbrtval = Math.cbrt(216);
        System.out.println("cube root : " + cbrtval);
 
    }
}


Java
// Java program explaining lang.MATH class methods
// floor(), hypot(), IEEEremainder(), log()
 
import java.lang.*;
public class NewClass
{
 
    public static void main(String[] args)
    {
        // Use of floor method
        double f1 = 30.56, f2 = -56.34;
        f1 =Math.floor(f1);
        System.out.println("Floor value of f1 : "+f1);
 
        f2 =Math.floor(f2);
        System.out.println("Floor value of f2 : "+f2);
        System.out.println("");
 
        // Use of hypot() method
        double p = 12, b = -5;
        double h = Math.hypot(p, b);
        System.out.println("Hypotenuse : "+h);
        System.out.println("");
 
        // Use of IEEEremainder() method
        double d1 = 105, d2 = 2;
        double r = Math.IEEEremainder(d1,d2);
        System.out.println("Remainder : "+r);
        System.out.println("");
         
        // Use of log() method
        double l = 10;
        l = Math.log(l);
        System.out.println("Log value of 10 : "+l);
         
    }
}


Java
// Java program explaining lang.Math class methods
// atan(), ceil(), copySign()
 
import java.math.*;
public class NewClass
{
    public static void main(String[] args)
    {
        // Use of atan() method
        double Atani = Math.atan(0);
        System.out.println("atan value of Atani : "+Atani);
        double x = Math.PI/2;
 
        // Use of toRadian() method
        x = Math.toRadians(x);
        double Atanj = Math.atan(x);
        System.out.println("atan value of Atanj : "+Atanj);
        System.out.println("");
 
 
        // Use of ceil() method
        double val = 15.34 ,ceilval;
        ceilval = Math.ceil(val);
        System.out.println("ceil value of val : "+ceilval);
        System.out.println("");
 
        double dblMag = val;
        double dblSign1 = 3;
        double dblSign2 = -3;
 
 
        // Use of copySign() method
        double result1 = Math.copySign(dblMag,dblSign1);
        System.out.println("copySign1 : "+result1);
 
        double result2 = Math.copySign(dblMag,dblSign2);
        System.out.println("copySign2 : "+result2);
         
    }
}


输出 :

Initial value of int  : -1
Initial value of int  : 0.5
Absolute value of int : 1
Absolute value of int : 0.5

acos value of Acosi : NaN
acos value of Acosj : 1.5159376794536454

4. asin() : Java.lang.Math.asin()方法返回传递的方法参数的反正弦值。返回的角度在 -pi/2 到 pi/2 的范围内。
反正弦是传递的参数的反正弦。
asin(arg) = arg 的正弦-1

特例 :

  • 结果为 NaN,如果参数为 NaN 或其绝对值大于 1。
  • 如果参数为零,则结果为零。

句法:

public static double asin(double arg)
Parameters:
arg - argument passed. 
Returns:
arc sine of the argument passed.

5. cbrt() : Java.lang.Math.cbrt()方法返回所传递参数的立方根。
特别点:

  • 如果参数为 NaN,则结果为 NaN。
  • 如果参数是无限的,则结果是与参数相同符号的无穷大。
  • 如果参数为零,则结果为零。

句法:

public static double cbrt(double arg)
Parameters:
arg - argument passed. 
Returns:
cube root of the argument passed

Java代码解释 lang.Math 类中的 asin()、cbrt() 方法。

Java

// Java program explaining lang.Math class methods
// asin(), cbrt()
 
import java.lang.*;
public class NewClass
{
 
    public static void main(String[] args)
    {
        int a = 1, b = 8;
        int radd = a+b;
 
        // Use of asin() method
        // Value greater than 1, so passing NaN
        double Asini = Math.asin(radd);
        System.out.println("asin value of Asini : " + Asini);
        double x = Math.PI;
 
        // Use of toRadian() method
        x = Math.toRadians(x);
        double Asinj = Math.asin(x);
        System.out.println("asin value of Asinj : " + Asinj);
        System.out.println("");
 
        // Use of cbrt() method
        double cbrtval = Math.cbrt(216);
        System.out.println("cube root : " + cbrtval);
 
    }
}

输出 :

asin value of Asini : NaN
asin value of Asinj : 0.054858647341251204

cube root : 6.0

6. floor() : Java.lang.Math.floor()方法返回参数的下限值,即最接近的整数值,小于或等于传递的参数。
例如:101.23 的底值 = 101

要点:如果传递了 NaN 或无限参数,则会产生相同的参数。

Syntax:
public static double floor(double arg)
Parameters:
arg - the argument whose floor value we need
Returns:closest possible value that is either less than 
                or equal to the argument passed

7. hypot() : Java.lang.Math.hypot(double p, double b)方法返回直角三角形的斜边在传递三角形的底和垂直作为参数。
斜边 = [垂直2 + 底2 ] 1/2

很重要的一点 :

  • 如果任一参数是无限的,则结果是正无穷大。
  • 如果任一参数为 NaN 且任一参数都不是无限的,则结果为 NaN。
Syntax:
public static double hypot(double p, double b)
Parameters:
p - perpendicular of the right triangle
b - base of the right triangle
Returns:
hypotenuse of the right triangle

8. IEEEremainder() : Java.lang.Math.IEEERemainder(double d1, double d2)方法通过对 IEEE 754 标准的两个参数应用余数运算来返回余数。
余值 = d1 – d2 * n
在哪里,
n = d1/d2 最接近的精确值

Syntax:
public static double IEEEremainder(double d1,double d2)
Parameters:
d1 - dividend 
d2 - divisor
Returns:
remainder when f1(dividend) is divided by(divisor)

9. log() : Java.lang.Math.log()方法返回传递参数的对数值。

Syntax:
public static double log(double arg)
Parameters:
arg - argument passed. 
Returns:
logarithmic value of the argument passed.

Java代码解释了 lang.Math 类中的 floor()、hypot()、IEEEremainder()、log() 方法。

Java

// Java program explaining lang.MATH class methods
// floor(), hypot(), IEEEremainder(), log()
 
import java.lang.*;
public class NewClass
{
 
    public static void main(String[] args)
    {
        // Use of floor method
        double f1 = 30.56, f2 = -56.34;
        f1 =Math.floor(f1);
        System.out.println("Floor value of f1 : "+f1);
 
        f2 =Math.floor(f2);
        System.out.println("Floor value of f2 : "+f2);
        System.out.println("");
 
        // Use of hypot() method
        double p = 12, b = -5;
        double h = Math.hypot(p, b);
        System.out.println("Hypotenuse : "+h);
        System.out.println("");
 
        // Use of IEEEremainder() method
        double d1 = 105, d2 = 2;
        double r = Math.IEEEremainder(d1,d2);
        System.out.println("Remainder : "+r);
        System.out.println("");
         
        // Use of log() method
        double l = 10;
        l = Math.log(l);
        System.out.println("Log value of 10 : "+l);
         
    }
}

输出 :

Floor value of f1 : 30.0
Floor value of f2 : -57.0

Hypotenuse : 13.0

Remainder : 1.0

Log value of 10 : 2.302585092994046

10. ceil() : Java.lang.Math.ceil(double a)方法返回大于或等于传递的参数的最小可能值。返回值是一个数学整数。

  • 如果返回的值已经是一个数学整数,结果是一样的。
  • 如果传递的参数是 NaN 或无穷大或零,则结果相同。
  • 如果传递的参数小于零但大于 -1.0,则结果为负零

句法:

public static double ceil(double arg)
Parameters:
arg - the argument value
Returns:
smallest possible value(mathematical integer)
which is either greater or equal to the argument passed

11. atan() : Java.lang.Math.atan()方法返回返回方法参数值的反正切值。返回的角度在 -pi/2 到 pi/2 的范围内。
arc tan 是传递的参数的逆 tan。
atan(arg) = tan arg 的倒数

特例 :

  • 如果传递的参数为 NaN 或其绝对值 > 1,则结果为 NaN。
  • 如果参数为零,则结果为零。

句法:

public static double atan(double a)
Parameters:
a - the argument whose arc tangent value we need.
    argument is taken as radian
Returns:
arc tan value of the argument.

12. copySign() : Java.lang.Math.copySign()方法返回第一个浮点参数,但具有第二个参数的符号。

句法:

public static double copySign(double m, double s)
                    or
public static float copySign(float m, float s)
Parameters:
m - magnitude 
s - sign 
Returns:
returns first argument with sign of second floating-point argument.

Java代码解释 lang.Math 类中的 atan()、ceil()、copySign() 方法。

Java

// Java program explaining lang.Math class methods
// atan(), ceil(), copySign()
 
import java.math.*;
public class NewClass
{
    public static void main(String[] args)
    {
        // Use of atan() method
        double Atani = Math.atan(0);
        System.out.println("atan value of Atani : "+Atani);
        double x = Math.PI/2;
 
        // Use of toRadian() method
        x = Math.toRadians(x);
        double Atanj = Math.atan(x);
        System.out.println("atan value of Atanj : "+Atanj);
        System.out.println("");
 
 
        // Use of ceil() method
        double val = 15.34 ,ceilval;
        ceilval = Math.ceil(val);
        System.out.println("ceil value of val : "+ceilval);
        System.out.println("");
 
        double dblMag = val;
        double dblSign1 = 3;
        double dblSign2 = -3;
 
 
        // Use of copySign() method
        double result1 = Math.copySign(dblMag,dblSign1);
        System.out.println("copySign1 : "+result1);
 
        double result2 = Math.copySign(dblMag,dblSign2);
        System.out.println("copySign2 : "+result2);
         
    }
}

输出 :

atan value of Atani : 0.0
atan value of Atanj : 0.0274087022410345

ceil value of val : 16.0

copySign1 : 15.34
copySign2 : -15.34

下一篇: Java.lang.math |设置 2