'What is the best way to realize high precision arithmetic calculations in JavaScript?
I'm doing an algorithm that checks if a number is prime for college, but I came across a connotation and calculation problem where I need to consider large numbers.
One of them was solved with the support of BigInt(), however in arithmetic calculations from a certain number of decimal places it ends up losing precision and consequently returning false true.
For example, multiplying 2 numbers ending in 1,3,7,9 always results in a number ending in 1,3,7,9, but from 3**34 onwards the calculations start to lose precision.
Is there any efficient way to solve this problem in JavaScript?
console.log(`
${3**32}, ${BigInt(3**32)}
${3**33}, ${BigInt(3**33)}
${3**34}, ${BigInt(3**34)}
${3**35}, ${BigInt(3**35)}
${3**37}, ${BigInt(3**37)}
${3*1597*3237*5549}, ${BigInt(3*1597*3237*5549)}
${3*1597*3237*5549*13213}, ${BigInt(3*1597*3237*5549*13213)}
${3*1597*3237*5549*13213*4543}, ${BigInt(3*1597*3237*5549*13213*4543)}
`);
Solution 1:[1]
You could.... not use Javascript ?
Outside of that, you could take Mike Cowlishaw's ANSI C implementation of arbitrary precision decimal arithmetic decNumber or decFloats package (http://speleotrove.com/decimal/#decNumber) and write Node.js bindings for it.
Mike Cowlishaw knows a thing or 3 about arithmetic:
In 1998 and 1999 he was also Project Editor and technical Chair for the ECMAScript (JavaScript) international standard (now ISO 16262).
From 1999 through 2009, he worked on new decimal arithmetic packages (including IBM’s BigDecimal class for Java and the decNumber C library), invented the Densely Packed Decimal encoding, and helped specify new decimal hardware architecture. He is the author of the General Decimal Arithmetic specifications, and was the Specification Lead for the decimal arithmetic enhancements included in Java 5 in 2004.
Mike championed the addition of the new decimal types and arithmetic to the IEEE 754 Standard for Floating-Point Arithmetic and to the C, C++, COBOL, and other languages (as well as in hardware), and remains active in the related work of a number of standards organizations, including ECMA, ISO, ANSI, IEEE, BSI, and W3C. He has been the Editor of the IEEE 754 standard since its first ballot in 2007 and through its publication in 2008 and as ISO/IEC/IEEE 60559 in 2011, and was a member of the Program Committee for the IEEE Symposium on Computer Arithmetic. IEEE 754 was further enhanced, with Mike as Editor, and the revised standard was published in 2019.
Solution 2:[2]
BigInt type numbers are recommended for this type of operation. I believed that BigInt() was just a conversion method, but is a numerical unit as well.
calculations must also be done using the same number format, and if a division results in a fraction, it is rounded down.
console.log(`
${3n**32n}
${3n**33n}
${3n**34n}
${3n**35n}
${3n**37n}
${3n*1597n*3237n*5549n}
${3n*1597n*3237n*5549n*13213n}
${3n*1597n*3237n*5549n*13213n*4543n}
${1n/3n}
${2n/3n}
`);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Nicholas Carey |
| Solution 2 | Rafael Lucas |
