Math和math.BigDecimal 的 那些事 ?

本文介绍了Java中Math类的基本数学运算,包括三角函数、对数、平方根等,并展示了如何使用这些方法。同时,讨论了浮点数精度问题,引出了BigDecimal类用于高精度计算的使用方法,强调了在商业计算中使用BigDecimal的重要性。
摘要由CSDN通过智能技术生成

前言

我们自己在进行算术运算时,不断的进行循环操作,思考,这样很费脑子啊,于是发现了百宝箱,里面藏了很多方法,都可以直接使用,很省事啊!

Math

类Math包含用于执行基本数字运算的方法,例如基本指数,对数,平方根和三角函数。

源码中的常量

public static final double E = 2.7182818284590452354;
public static final double PI = 3.14159265358979323846;
private static final double DEGREES_TO_RADIANS = 0.017453292519943295;
private static final double RADIANS_TO_DEGREES = 57.29577951308232;

常量的使用

    public static void main(String[] args) {
        //3.141592653589793
        System.out.println(Math.PI);
    }

源码中的方法

三角函数

	public static double sin(double a) {
        return StrictMath.sin(a);
        }   
    public static double cos(double a) {
        return StrictMath.cos(a); 
        } 
	public static double tan(double a) {
        return StrictMath.tan(a);
        }
	public static double asin(double a) {
        return StrictMath.asin(a);
        }
	public static double acos(double a) {
        return StrictMath.acos(a);
        }
	public static double atan(double a) {
        return StrictMath.atan(a);
        }
    public static double toDegrees(double angrad) {
        return angrad * RADIANS_TO_DEGREES;
    }
    public static double atan2(double y, double x) {
        return StrictMath.atan2(y, x); // default impl. delegates to StrictMath
    }

三角函数使用:

        double b = Math.PI/2;
        //1.0
        System.out.println(Math.sin(b));

基本算术

	public static double log10(double a) {
        return StrictMath.log10(a);
        }
    public static double sqrt(double a) {
        return StrictMath.sqrt(a); 
    }
    //根据IEEE 754标准规定,计算两个参数的余数运算。 
    public static double IEEEremainder(double f1, double f2) {
        return StrictMath.IEEEremainder(f1, f2); // delegate to StrictMath
    }
    //返回大于或等于参数且等于数学整数的最小值(最接近负无穷大) double 。 对上取整 
    public static double ceil(double a) {
        return StrictMath.ceil(a);
        }
    //返回小于或等于参数且等于数学整数的最大值(最接近正无穷大) double 。 对下取整
	public static double floor(double a) {
        return StrictMath.floor(a);
        }
    //返回与 double值最接近的 double值,该值等于数学整数。
    public static double rint(double a) {
        return StrictMath.rint(a);
        }
    //取绝对值
  	public static long abs(long a) {
        return (a < 0) ? -a : a;
    }
    //返回与参数最接近的long ,其中四度为正无穷大 就是数学中的四舍五入
    public static long round​(double a);
    

使用:

		int a = -5;
        //5
        System.out.println(Math.abs(a));
        double c = 2.5;
        //3.0 
        System.out.println(Math.ceil(c));
       	//3
        System.out.println(Math.round(c));

在这里插入图片描述

Math.BigDecimal

Java在java.math包中提供的API类BigDecimal,用来对超过16位有效位的数进行精确的运算。双精度浮点型变量double可以处理16位有效数。在实际应用中,需要对更大或者更小的数进行运算和处理。float和double只能用来做科学计算或者是工程计算,在商业计算中要java.math.BigDecimal。BigDecimal所创建的是对象,我们不能使用传统的+、-、*、/等算术运算符直接对其对象进行数学运算,而必须调用其相对应的方法。方法中的参数也必须是BigDecimal的对象。构造器是类的特殊方法,专门用来创建对象,特别是带有参数的对象。

因为不论是float 还是double都是浮点数,而计算机是二进制的,浮点数会失去一定的精确度。注:根本原因是:十进制值通常没有完全相同的二进制表示形式;十进制数的二进制表示形式可能不精确。只能无限接近于那个值,这就导致精度确实.

 public static void main(String[] args) {
        System.out.println(0.1+0.3);
        System.out.println(0.6-0.2);
        System.out.println(0.6*0.2);
        System.out.println(0.6/0.2);
        /**
         * 0.4
         * 0.39999999999999997
         * 0.12
         * 2.9999999999999996
         */
    }

正确使用:

   		BigDecimal a = new BigDecimal("6");
        BigDecimal b = new BigDecimal("2");
        System.out.println(a.add(b));
        System.out.println(a.subtract(b));
        System.out.println(a.multiply(b));
        System.out.println(a.divide(b));
        /**
         * 8
         * 4
         * 12
         * 3
         */
        double c =3.3;
        float d =5.0f;
        BigDecimal s = new BigDecimal(Double.toString(c));
        BigDecimal f = new BigDecimal(Float.toString(d));
        //16.50
        System.out.println(s.multiply(f));
        
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我想去拉萨。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值