js、java小数点以下の計算精度問題

4532 ワード

js精度問題
javascriptは、小数の演算を計算する時、精度の問題があります.これはコンピュータのバイナリ計算と関係があります.次の2つの案により、
1)文字列を通して、その後パーズFloatを計算しても良いです.
2)公共の加減乗除の方法を書く
/**
* javascript calc
* 
*        :  JS    BUG  
*       :    
*      :  2012-12-21  ,      BUG
* 
*/

function accDiv(arg1, arg2) {

    var t1 = 0, t2 = 0, t3 = 0, r1, r2;

    try { t1 = arg1.toString().split(".")[1].length } catch (e) { }

    try { t2 = arg2.toString().split(".")[1].length } catch (e) { }

    r1 = Number(arg1.toString().replace(".", ""))

    r2 = Number(arg2.toString().replace(".", ""))

    if (r2 == 0)
        return 0;

    var result = String(r1 / r2);

    try { t3 = result.toString().split(".")[1].length } catch (e) { }

    var index = t2 - t1 - t3;

    if (index < 0) {
        result = result.replace(".", "");

        while (result.length <= Math.abs(index)) {
            result = '0' + result;
        }

        var start = result.substring(0, result.length + index);
        var end = result.substring(result.length + index, result.length);

        result = start + '.' + end;

        return Number(result);
    }
    else if (index > 0) {
        result = result.replace(".", "");

        while (result.length <= Math.abs(index)) {
            result += '0';
        }
        return Number(result);
    }
    else return Number(result.replace(".", ""));

}


// Number      div  ,        。

Number.prototype.div = function (arg) {
    ///	<summary>
    ///	      
    ///	</summary>
    return accDiv(this, arg);

}
function accMul(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)

}

Number.prototype.mul = function (arg) {
    ///	<summary>
    ///	      
    ///	</summary>
    return accMul(arg, this);

}

function accAdd(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

}

Number.prototype.add = function (arg) {
    ///	<summary>
    ///	      
    ///	</summary>
    return accAdd(arg, this);

}
java精度問題
java打算はBigDecimal(String)を通じて、次のようになります.
/**
	 *                
	 * 
	 * @param v1
	 * @param v2
	 * @return
	 */
	public static double add(double v1, double v2) {
		BigDecimal b1 = new BigDecimal(Double.toString(v1));
		BigDecimal b2 = new BigDecimal(Double.toString(v2));
		return b1.add(b2).doubleValue();
	}

	/**
	 *                
	 * 
	 * @param v1
	 * @param v2
	 * @return
	 */
	public static double subtract(double v1, double v2) {
		BigDecimal b1 = new BigDecimal(Double.toString(v1));
		BigDecimal b2 = new BigDecimal(Double.toString(v2));
		return b1.subtract(b2).doubleValue();
	}
	/**
	 *                    
	 * @param v1
	 * @param v2
	 * @param len        
	 * @return
	 */
	public static double div(double v1, double v2,int len) {//        
		 BigDecimal b1 = new BigDecimal(v1);  
		 BigDecimal b2 = new BigDecimal(v2);  
		 return b1.divide(b2,len,BigDecimal.ROUND_HALF_UP).doubleValue();  
	}  
    
    /**
     *                 
     * 
     * @param v1
     * @param v2
     * @return
     */
    public static double multiply(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.multiply(b2).doubleValue();
    }