Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

divide(4750.49269435, 4)结果错误 #4

Closed
natee opened this issue Oct 26, 2017 · 1 comment
Closed

divide(4750.49269435, 4)结果错误 #4

natee opened this issue Oct 26, 2017 · 1 comment

Comments

@natee
Copy link

natee commented Oct 26, 2017

/**
 * 精确乘法
 */
function times(num1, num2) {
    // var num1Changed = Number(num1.toString().replace('.', ''));
    // var num2Changed = Number(num2.toString().replace('.', ''));
    // var baseNum = digitLength(num1) + digitLength(num2);
    var dl1 = digitLength(num1);
    var dl2 = digitLength(num2);
    var num1Changed = num1 * Math.pow(10, dl1);
    var num2Changed = num2 * Math.pow(10, dl2);
    var baseNum = dl1 + dl2;
    return num1Changed * num2Changed / Math.pow(10, baseNum);
}

/**
 * 精确除法
 */
function divide(num1, num2) {
    // var num1Changed = Number(num1.toString().replace('.', ''));
    // var num2Changed = Number(num2.toString().replace('.', ''));
    var dl1 = digitLength(num1);
    var dl2 = digitLength(num2);
    var num1Changed = num1 * Math.pow(10, dl1);
    var num2Changed = num2 * Math.pow(10, dl2);
    return times((num1Changed / num2Changed), Math.pow(10, dl2 - dl1));
}
@camsong
Copy link
Member

camsong commented Oct 29, 2017

Thanks for reporting, fixed by v1.1.6

@camsong camsong closed this as completed Oct 29, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants