Java java.math类和它的方法

来源:这里教程网 时间:2026-02-17 20:57:08 作者:

Java java.math类和它的方法

Math类提供了一些数学函数来执行基本的数字运算,如指数、对数、平方根和三角函数,cosh、sin、tan、abs、bitLength、multiply等等。数学类的函数的实现不会返回位对位的相同结果。因此,要进行更好的实现。

类的声明

public final class Math  extends Object

本集解释了以下方法:

方法

signum() :java.math.signum()**方法返回所传参数的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)
参数:
x – 我们需要的参数的符号值
返回:
x的符号值

round() :java.math.round()方法将传递的参数四舍五入到最接近的小数位。
**注意:
结果是0,如果参数是NaN。
语法:
public static long round(long arg)

public static double round(double arg)
参数:
arg – 需要舍弃的参数
返回:
参数的四舍五入值

max() : java.math.max(double v1, double v2)方法返回两个传递的参数值中的较大值。
这个方法只是使用大小进行比较,不考虑任何符号。
语法:
public static double max(double v1, double v2)
参数:
v1 – 第一个值
v2 – 第二个值
返回:
基于哪个数字大,就返回v1或v2。
如果v1 = v2,它可以返回这两个中的任何一个。

解释数学类中signum()、round()、max()方法的Java代码

// Java code explaining the 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.0Signum of -23.34 = -1.0Round off 10.4556  = 10.0Round off 23.34789 = -23.0Max b/w r1 and r2 = -23.0
    log1p() : java.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.math.ulp()方法返回最小精度单位(ulp),即两个浮点数之间的最小距离。
    在这里,它是参数和下一个较大值之间的最小距离。

语法:

public static double ulp(double arg)              orpublic static float ulp(float arg)

参数:
arg – 通过的参数。
返回值:
参数与下一个较大值之间的最小距离。

解释数学类中ulp(), log1p()方法的Java代码

// Java code explaining the 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-15ulp of -23.34789 : 3.552713678800501E-15Log of (1 + 99)  : 4.605170185988092Log of 100       : 4.605170185988092

相关推荐