C/C++教程

3.2. 数学类(Math、BigInteger、BigDecimal)

本文主要是介绍3.2. 数学类(Math、BigInteger、BigDecimal),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1. Math类

Math类提供了一些基本的数学函数,如求平方根、绝对值、三角函数等。它是一个final类,并且所有的方法都是static的,因此无需创建对象,直接使用类名调用方法即可。

以下是Math类的一些常用方法:

  • abs(double a):返回参数的绝对值。
  • ceil(double a):返回大于或等于参数的最小整数值。
  • floor(double a):返回小于或等于参数的最大整数值。
  • round(double a):返回参数四舍五入后的整数值。
  • max(double a, double b):返回两个参数中的最大值。
  • min(double a, double b):返回两个参数中的最小值。
  • sqrt(double a):返回参数的平方根。
  • pow(double a, double b):返回ab次幂。
  • sin(double a):返回参数的正弦值。
  • cos(double a):返回参数的余弦值。
  • tan(double a):返回参数的正切值。

2. BigInteger类

BigInteger类表示任意精度的整数。在处理大整数时,intlong的范围可能不够用,此时可以使用BigInteger类。BigInteger类提供了大量的方法来操作大整数,如加法、减法、乘法、除法等。

以下是创建BigInteger对象的一些方法:

  • BigInteger(String val):根据字符串创建BigInteger对象。
  • valueOf(long val):返回一个等于指定long值的BigInteger对象。

以下是BigInteger类的一些常用方法:

  • add(BigInteger val):返回两个BigInteger对象的和。
  • subtract(BigInteger val):返回两个BigInteger对象的差。
  • multiply(BigInteger val):返回两个BigInteger对象的积。
  • divide(BigInteger val):返回两个BigInteger对象的商。
  • mod(BigInteger val):返回两个BigInteger对象的余数。
  • pow(int exponent):返回当前BigInteger对象的指定次幂。

3. BigDecimal类

BigDecimal类表示任意精度的小数。在处理需要高精度计算的小数时,floatdouble的范围和精度可能不够用,此时可以使用BigDecimal类。BigDecimal类提供了大量的方法来操作小数,如加法、减法、乘法、除法等。

以下是创建BigDecimal对象的一些方法:

  • BigDecimal(String val):根据字符串创建BigDecimal对象。
  • valueOf(double val):返回一个等于指定double值的BigDecimal对象。

以下是BigDecimal类的一些常用方法:

  • add(BigDecimal val):返回两个BigDecimal对象的和。
  • subtract(BigDecimal val):返回两个BigDecimal对象的差。
  • multiply(BigDecimal val):返回两个BigDecimal对象的积。
  • divide(BigDecimal val, int scale, RoundingMode roundingMode):返回两个BigDecimal对象的商,保留指定小数位数,并使用指定的舍入模式。
  • setScale(int newScale, RoundingMode roundingMode):返回一个BigDecimal对象,保留指定小数位数,并使用指定的舍入模式。

4. 示例

下面是一个使用MathBigIntegerBigDecimal类的示例:

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;

public class Main {
    public static void main(String[] args) {
        // 使用Math类
        System.out.println("绝对值:" + Math.abs(-10)); // 输出 "绝对值:10"
        System.out.println("平方根:" + Math.sqrt(9)); // 输出 "平方根:3.0"
        System.out.println("最大值:" + Math.max(3, 7)); // 输出 "最大值:7"

        // 使用BigInteger类
        BigInteger num1 = new BigInteger("987654321098765432");
        BigInteger num2 = new BigInteger("123456789012345678");
        System.out.println("大整数相加:" + num1.add(num2)); // 输出 "大整数相加:1111111110111111110"
        System.out.println("大整数相减:" + num1.subtract(num2)); // 输出 "大整数相减:864197532086419754"
        System.out.println("大整数相乘:" + num1.multiply(num2)); // 输出 "大整数相乘:121932631137021795435340303682"

        // 使用BigDecimal类
        BigDecimal decimal1 = new BigDecimal("123.456");
        BigDecimal decimal2 = new BigDecimal("789.012");
        System.out.println("高精度小数相加:" + decimal1.add(decimal2)); // 输出 "高精度小数相加:912.468"
        System.out.println("高精度小数相减:" + decimal1.subtract(decimal2)); // 输出 "高精度小数相减:-665.556"

        // 高精度小数相乘
        BigDecimal decimal3 = decimal1.multiply(decimal2);
        System.out.println("高精度小数相乘:" + decimal3); // 输出 "高精度小数相乘:97421.697632"

        // 高精度小数相除
        BigDecimal decimal4 = decimal1.divide(decimal2, 5, RoundingMode.HALF_UP);
        System.out.println("高精度小数相除:" + decimal4); // 输出 "高精度小数相除:0.15649"

        // 设置小数位数和舍入模式
        BigDecimal decimal5 = decimal3.setScale(2, RoundingMode.HALF_UP);
        System.out.println("高精度小数保留2位小数:" + decimal5); // 输出 "高精度小数保留2位小数:97421.70"
    }
}

通过这个示例,您可以了解到MathBigIntegerBigDecimal的基本用法和常用方法。在实际编程过程中,您会经常使用这些类来处理数学计算。希望这个介绍能帮助您更好地学习和理解Java中的数学类。

这篇关于3.2. 数学类(Math、BigInteger、BigDecimal)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!