Type a very large number into a calculator or into JavaScript and the last digits may change. Ask for 171 factorial and you may get “Infinity”. These are not bugs in the arithmetic. They come from how computers store numbers.
How 64-bit floating point stores numbers
Most calculators and all JavaScript numbers use a 64-bit floating-point format. It keeps about 15 to 17 significant decimal digits (53 binary digits of precision) plus an exponent that sets the scale. That makes it fast and able to cover huge ranges, but not exact for every number.
The safe-integer limit: 2^53 − 1
Whole numbers are exact only up to 9,007,199,254,740,991 (2^53 − 1). In JavaScript this is Number.MAX_SAFE_INTEGER. Above it, not every integer can be stored, so 9007199254740993 is shown as 9007199254740992. Everyday decimals have the same issue in a different form: 0.1 + 0.2 gives 0.30000000000000004 because 0.1 and 0.2 cannot be stored exactly in binary.
Factorial overflow at 171
The largest value a 64-bit float can hold is about 1.8 × 10^308. 170! is about 7.26 × 10^306 and still fits, but 171! is too large and turns into Infinity. A calculator that shows exact digits for 171! or 1000! must use a different method.
Big IDs in JSON
Services with 64-bit numeric IDs can send values larger than 2^53 in JSON. A JavaScript program that reads them as numbers can quietly change the last digits. The safer fix is to send such IDs as strings. You can inspect a payload with the JSON Formatter, and see how to find and fix JSON problems.
Arbitrary-precision arithmetic
To get exact answers for huge integers, software stores the number as a list of digits (or chunks of digits) and does the arithmetic the way you do it on paper, like long multiplication. In JavaScript this is the BigInt type, written with an n suffix, for example 12345678901234567890n * 98765432109876543210n gives exactly 1219326311370217952237463801111263526900. It is slower than normal numbers and BigInt handles whole numbers only, not decimals.
Quick checklist
- Whole numbers up to about 9 quadrillion are safe in normal numbers.
- Beyond that, use BigInt or strings.
- For money, avoid binary floats: use whole cents or a decimal library.
- If a result shows “e+”, the calculator switched to scientific notation. See the Scientific Notation Calculator.
Try it
Use the Big Number Calculator to add, multiply and raise huge integers exactly. Also useful: the Exponent Calculator, the Unix Timestamp Converter and the Hash Generator.
Leave a Reply