Is it possible to achieve arbitrary-precision arithmetic with no rounding issues in JavaScript?

Hydrothermal

I've tried big.js, bignumber.js, and decimal.js; they all work reasonably well up to a certain point, but fall short when I need to do arbitrary-precision calculations with large enough numbers of "odd" digits (my current test case is 31435517643980 * (1 / 31435517643980) === 1). I am open to any solution that allows me to process expressions like this, including calls to an external API. I'm currently looking at Wolfram|Alpha's API, but the 2000 calls/month limit is a restriction I'd like to avoid, because my application is going to be making quite a few calls.

If this is the wrong SE site for this question, please let me know and/or move it.

markasoftware

Possibly the most common way to do this is simply multiply both numbers by the same multiplier to make them have no decimals, and then do the operation, then divide again. Here's a crude implementation:

function getDigits(n){
    return n.toString().substring(n.toString().indexOf('.')+1).length;
}
function xNums(n1,n2){
    var highRes=(n1*Math.pow(10,getDigits(n1))*(n2*Math.pow(10,getDigits(n2))));
    return highRes/Math.pow(10,getDigits(n1))/Math.pow(10,getDigits(n2));
}

Then, run xNums(31435517643980,(1 / 31435517643980))===1. Works for me in Chrome

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Arbitrary precision arithmetic with very big factorials

From Dev

Arbitrary precision arithmetic with very big factorials

From Dev

BBP-Algorithm in Python - Working with Arbitrary-precision arithmetic

From Dev

Calculating pi without arbitrary precision and only basic arithmetic

From Dev

Calculating pi without arbitrary precision and only basic arithmetic

From Dev

How to achieve arbitrary chain on function call in javascript?

From Dev

Is it possible to fix rounding issues when pixelizing UV coordinates?

From Dev

Rounding by arbitrary step

From Dev

Lodash rounding precision

From Dev

Rounding to a fixed relative precision

From Dev

LLVM arbitrary precision integer

From Dev

Python mpmath not arbitrary precision?

From Dev

LLVM arbitrary precision integer

From Dev

awk high precision arithmetic

From Dev

awk high precision arithmetic

From Dev

How to achieve $.width() with precision

From Dev

How to achieve collision precision

From Dev

Python issues with rounding

From Dev

Rounding and trimming digits with specified precision

From Dev

How to set precision without rounding?

From Dev

Fixed precision vs. arbitrary precision

From Dev

arbitrary floating precision number to string

From Dev

"Multiplication of Arbitrary Precision Numbers" in Scheme

From Dev

stripe node arbitrary precision number

From Dev

Arithmetic precision problems with large numbers

From Dev

Bash arithmetic issues

From Dev

Saturating arithmetic issues with warnings

From Dev

Issues with C++ Precision

From Dev

Rotation precision issues