📜  Java.math 类及其方法 |设置 3

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

Java.math 类及其方法 |设置 3

Java.math 类及其方法 |设置 1
Java.math 类及其方法 |设置 2

java.math 类方法1

  • ceil() : Java.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
  • atan() : Java.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.
  • copySign() : Java.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 second argument with sign of first floating-point argument.

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

Java
// Java program explaining 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);
         
    }
}


Java
// Java program explaining 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);
 
    }
}


Java
// Java program explaining 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);
 
    }
}


输出:

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

ceil value of val : 16.0

copySign1 : 15.34
copySign2 : -15.34
  • cosh() : Java.math.cosh()方法返回所传递参数的双曲余弦值。
    特别案例 :
    • 如果参数为 NaN,则结果为 NaN。
    • 如果参数为零,则结果为 1.0。
    • 如果参数是无限的,则结果是 +ve infinity。

句法:

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.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.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代码解释了 Math 类中的 exp()、decrementExact()、cosh() 方法。

Java

// Java program explaining 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.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.math.log10()方法返回传递参数的以 10 为底的对数值。
Syntax:
public static double log(double arg)
Parameters:
arg - argument passed. 
Returns:
base10 logarithmic value of the argument passed.
  • pow() : Java.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

解释 Math 类中的 incrementExact()、log10()、pow() 方法的Java代码。

Java

// Java program explaining 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

注意:不需要创建新对象来调用这些方法,因为上面讨论的 Math 类方法是静态的。