加减乘除精确方法

加减乘除精确方法

  • 能精确到: 0.1 + 0.2 = 0.3哦
转载大神, 请各位多多指教
 /**
 * 加法
 * */
    function add (arg1, arg2) {
        var r1, r2, m, c;
        try {
            r1 = arg1.toString().split(".")[1].length
        } catch (e) {
            r1 = 0
        }
        try {
            r2 = arg2.toString().split(".")[1].length
        } catch (e) {
            r2 = 0
        }
        c = Math.abs(r1 - r2);
        m = Math.pow(10, Math.max(r1, r2))
        if (c > 0) {
            var cm = Math.pow(10, c);
            if (r1 > r2) {
                arg1 = Number(arg1.toString().replace(".", ""));
                arg2 = Number(arg2.toString().replace(".", "")) * cm;
            } else {
                arg1 = Number(arg1.toString().replace(".", "")) * cm;
                arg2 = Number(arg2.toString().replace(".", ""));
            }
        } else {
            arg1 = Number(arg1.toString().replace(".", ""));
            arg2 = Number(arg2.toString().replace(".", ""));
        }
        return (arg1 + arg2) / m
    }


    /**
     * 减法
     */
    function minus (arg1, arg2) {
        var r1, r2, m, n;
        try {
            r1 = arg1.toString().split(".")[1].length
        } catch (e) {
            r1 = 0
        }
        try {
            r2 = arg2.toString().split(".")[1].length
        } catch (e) {
            r2 = 0
        }
        m = Math.pow(10, Math.max(r1, r2));
        //last modify by deeka
        //动态控制精度长度
        n = (r1 >= r2) ? r1 : r2;
        return ((arg1 * m - arg2 * m) / m).toFixed(n);
    }


    /**
    * 乘法
    */
     function Multipy(arg1, arg2) {
         var m = 0,
             s1 = arg1.toString(),
             s2 = arg2.toString();
         try {
             m += s1.split(".")[1].length
         } catch (e) {}
         try {
             m += s2.split(".")[1].length
         } catch (e) {}
         return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)
     }


     /**
      * 除法
      */
     function Divide(arg1, arg2) {
         var t1 = 0,
             t2 = 0,
             r1, r2;
         try {
             t1 = arg1.toString().split(".")[1].length
         } catch (e) {}
         try {
             t2 = arg2.toString().split(".")[1].length
         } catch (e) {}
         with(Math) {
             r1 = Number(arg1.toString().replace(".", ""))
             r2 = Number(arg2.toString().replace(".", ""))
             return (r1 / r2) * pow(10, t2 - t1);
         }
     }
ps: 转载大神, 请各位多多指教
Java 中,实现加减乘除和求商有多种方式,以下分别介绍: - **基本数据类型的运算**:对于基本数据类型,如`int`、`double`等,可以直接使用`+`、`-`、`*`、`/`运算符进行加减乘除和求商操作。 ```java public class BasicArithmetic { public static void main(String[] args) { int a = 10; int b = 3; int sum = a + b; int difference = a - b; int product = a * b; int quotient = a / b; System.out.println("Sum: " + sum); System.out.println("Difference: " + difference); System.out.println("Product: " + product); System.out.println("Quotient: " + quotient); } } ``` - **使用`BigDecimal`进行精确计算**:对于需要高精度计算的场景,可使用`java.math`包中的`BigDecimal`类。由于`BigDecimal`是对象,不能直接使用算术运算符,需调用相应的方法,如`add()`、`subtract()`、`multiply()`和`divide()` [^2]。 ```java import java.math.BigDecimal; public class BigDecimalArithmetic { public static void main(String[] args) { BigDecimal num1 = new BigDecimal("10.5"); BigDecimal num2 = new BigDecimal("3.2"); BigDecimal sum = num1.add(num2); BigDecimal difference = num1.subtract(num2); BigDecimal product = num1.multiply(num2); // 指定精度和舍入模式 BigDecimal quotient = num1.divide(num2, 2, BigDecimal.ROUND_HALF_UP); System.out.println("Sum: " + sum); System.out.println("Difference: " + difference); System.out.println("Product: " + product); System.out.println("Quotient: " + quotient); } } ``` - **使用位运算实现加减乘除**:可以使用位运算来实现加减乘除的功能。例如,使用位运算实现两数相除的力扣题目(29. 两数相除),要求不使用乘法、除法和取余运算 [^3]。 ```java public class BitwiseDivision { public int divide(int dividend, int divisor) { // 处理溢出情况 if (dividend == Integer.MIN_VALUE && divisor == -1) { return Integer.MAX_VALUE; } int sign = (dividend > 0) ^ (divisor > 0) ? -1 : 1; long ldividend = Math.abs((long) dividend); long ldivisor = Math.abs((long) divisor); int result = 0; while (ldividend >= ldivisor) { long temp = ldivisor; long multiple = 1; while (ldividend >= (temp << 1)) { temp <<= 1; multiple <<= 1; } ldividend -= temp; result += multiple; } return sign * result; } } ``` - **使用复数类实现复数的加减乘除**:定义一个复数类`Complex`,重载运算符`+`、`-`、`*`、`/`(在 Java 中通过方法实现),用于复数的加减乘除 [^1]。 ```java class Complex { private double real; private double imag; public Complex(double real, double imag) { this.real = real; this.imag = imag; } public Complex add(Complex other) { return new Complex(this.real + other.real, this.imag + other.imag); } public Complex subtract(Complex other) { return new Complex(this.real - other.real, this.imag - other.imag); } public Complex multiply(Complex other) { double newReal = this.real * other.real - this.imag * other.imag; double newImag = this.real * other.imag + this.imag * other.real; return new Complex(newReal, newImag); } public Complex divide(Complex other) { double denominator = other.real * other.real + other.imag * other.imag; double newReal = (this.real * other.real + this.imag * other.imag) / denominator; double newImag = (this.imag * other.real - this.real * other.imag) / denominator; return new Complex(newReal, newImag); } @Override public String toString() { return real + " + " + imag + "i"; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值