Java数学处理类

1,数学运算

Math类:

在Math类中提供了众多数学函数方法,主要包括三角函数方法、指数函数方法、取整函数方法、取最大值、最小值以及平均值函数方法,这些方法都被定义为static形式,所以在程序中应用比较简便。可以使用如下形式调用:

Math.数学方法

三角函数方法:

  • public static double sin(double a):返回角的三角正弦。
  • public static double cos(double a):返回角的三角余弦。
  • public static double tan(double a):返回角的三角正切。
  • public static double asin(double a):返回一个值的反正弦。
  • public static double acos(double a):返回一个值的反余弦。
  • public static double atan(double a):返回一个值的反正切。

以上每个方法的参数和返回值都是double型的。

指数函数方法

  • public static double exp(double a):用于获取e 的a次方,即取e”。
  • public static double log10(double a):用于取底数为10的对数。
  • public static double sqrt(double a):用于取a的平方根,其中a的值不能为负值。
  • public static double cbrt(double a):用于取a的立方根。
  • public static double pow(double a,double b):用于取a 的b次方。

取整函数方法:

  • public static double ceil(double a):返回大于等于参数的最小整数。
  • public static double floor(double a):返回小于等于参数的最大整数。
  • public static double rint(double a):返回与参数最接近的整数,如果两个同为整数且同样接近,则结果取偶数。
  • public static int round(float a):将参数加上0.5后返回与参数最近的整数。
  • public static long round(double a):将参数加上0.5后返回与参数最近的整数,然后强制转换为长整型。

取最大值、最小值、绝对值函数方法

在程序中最常用的方法就是取最大值、最小值、绝对值等,在Math类中包括的这些操作方法如下。

  • public static double max(double a,double b):取a与b之间的最大值。
  • public static int min(int a,int b):取a与b之间的最小值,参数为整型。
  • public static long min(long a,long b):取a与b之间的最小值,参数为长整型。√
  • public static double min(double a,double b):取a与b之间的最小值,参数为双精度型。public static int abs(int a):返回整型参数的绝对值。
  • public static long abs(long a):返回长整型参数的绝对值。public static float abs(float a):返回浮点型参数的绝对值。
  • public static double abs(double a):返回双精度型参数的绝对值。

2,随机数

Math.random()方法

在Math类中存在一个random()方法,用于产生随机数字,这个方法默认生成大于等于0.0且小于1.0的double型随机数,即0<=Math.random()<1.0,虽然 Math.random()方法只可以产生(~1之间的double型数字,其实只要在 Math.random()语句上稍加处理,就可以使用这个方法产生任意范围的随机数。

Random类

除了Math类中的random()方法可以获取随机数之外,Java中还提供了一种可以获取随机数的方式,那就是java.util.Random类。可以通过实例化一个Random对象创建一个随机数生成器。

Random r = new Random();

其中,r是指Random对象。
以这种方式实例化对象时,Java编译器以系统当前时间作为随机数生成器的种子,因为每时每刻的时间不可能相同,所以产生的随机数将不同,但是如果运行速度太快,也会产生两次运行结果相同的随机数。同时也可以在实例化 Random类对象时,设置随机数生成器的种子。语法如下:

Random r = new Random(seedVal);
//seedValue:随机数生成器的种子。

在Random类中提供了获取各种数据类型随机数的方法:

  • public int nextInt():返回一个随机整数。
  • public int nextInt(int n):返回大于等于0且小于n的随机整数。
  • public long nextLong():返回一个随机长整型值。
  • public boolean nextBoolean():返回一个随机布尔型值。
  • public float nextFloat():返回一个随机浮点型值。
  • public double nextDouble():返回一个随机双精度型值。
  • public double nextGaussian():返回一个概率密度为高斯分布的双精度值。

3,大数字运算

BigInteger类

BigInteger支持任意精度的整数,也就是说在运算中 BigInteger类型可以准确地表示任何大小的整数值而不会丢失任何信息。使用BigInteger类,可以实例化一个 BigInteger对象,并自动调用相应的构造函数。在 BigInteger类中封装了多种操作,除了基本的加、减、乘、除操作之外,还提供了绝对值、相反数、最大公约数以及判断是否为质数等操作。使用BigInteger类,可以实例化一个 BigInteger对象,并自动调用相应的构造函数。

BigInteger类具有很多构造函数,但最直接的一种方式是参数以字符串形式代表要处理的数字。

public Biglnteger(String val)
//val是十进制字符串

如果将⒉转换为BigInteger类型,可以使用以下语句进行初始化操作:

Biglnteger twoinstance=new Biginteger("2");

BigInteger类中常用的几种运算方法

  • public BigInteger add(BigInteger val):做加法运算。
  • public BigInteger subtract(BigInteger val):做减法运算。
  • public BigInteger multiply(BigInteger val):做乘法运算。
  • public BigInteger divide(BigInteger val):做除法运算。
  • public BigInteger remainder(BigInteger val):做取余操作。
  • public BigInteger[] divideAndRemainder(BigInteger val):用数组返回余数和商,结果数组中第一个值为商,第二个值为余数。
  • public BigInteger pow(int exponent):进行取参数的exponent次方操作。
  • public BigInteger negate():取相反数。
  • public BigInteger shiftLeft(int n):将数字左移n位,如果n为负数,做右移操作。
  • public BigInteger shiftRight(int n):将数字右移n位,如果n为负数,做左移操作
  • public BigInteger and(BigInteger val):做与操作。
  • public BigInteger or(BigInteger val):做或操作。
  • public int compareTo(BigInteger val):做数字比较操作。
  • public boolean equals(Object x):当参数x是 BigInteger类型的数字并且数值相等时,返回true。
  • public BigInteger min(BigInteger val):返回较小的数值。
  • public BigInteger max(BigInteger val):返回较大的数值。

BigDecimal类

在 BigDecimal类中常用的两个构造方法如下。

  • public BigDecimal(double val):实例化时将双精度型转换为BigDecimal类型。
  • public BigDecimal(String val):实例化时将字符串形式转换为BigDecimal类型。

下面列举了BigDecimal类中实现加、减、乘、除的方法。

  • public BigDecimal add(BigDecimal augend):做加法操作。
  • public BigDecimal subtract(BigDecimal subtrahend):做减法操作。
  • public BigDecimal multiply(BigDecimal multiplicand):做乘法操作。
  • public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode):做除法操作,方法中3个参数分别代表除数、商的小数点后的位数、近似处理模式。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值