Math和math.BigDecimal 的 那些事 ?

前言

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

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));
        

猜你喜欢

转载自blog.csdn.net/AzirBoDa/article/details/112918213