DEV Community

fei mai
fei mai

Posted on

Our Calculator Is Exact to 30 Digits. Its "Decimal Preview" Said Zero.

The input that broke it was 1,000,000,000. That's not a typo and it's not exotic — it's a billion, the kind of number that shows up in a currency conversion, a population question, a scale factor.

Enter 1 / 1000000000 = c / 1 and leave the third box empty. The calculator answers c = 1/1000000000, which is correct, exact, and better than most calculators manage. Then, on the line underneath, it adds a decimal preview.

The preview says zero.

How the 77 inputs were built

Our proportion calculator is a static page with no backend. All of the arithmetic lives in one ES module, math.mjs, 264 lines, using BigInt fractions so that a third stays a third. The slope calculator on the sister site imports the exact same file — so a test of one site is a test of both, and so is a bug.

The harness: Node 22.22.0 on Windows, 77 hand-built inputs, five entry modes. Number parsing, solve and check, splitting a total in a ratio, scaling, and the three slope modes. Then a second implementation in Python using fractions.Fraction — no shared code, written by me from the spec rather than from the JavaScript — recomputes every answer and compares as exact rationals. The comparison is on integers, not floats, so nothing can hide inside a rounding error.

Two runs. Same 77 inputs, same 75 right answers.

Mode Inputs Correct Wrong
Number parsing 14 12 0
Proportion, solve and check 20 20 0
Splitting a total in a ratio 10 10 0
Scaling 6 6 0
Slope, all three entry modes 18 18 0
Extreme magnitudes 9 7 2
Total 77 75 2

Three things that flipped my assumptions

The arithmetic is not the weak part. The label is.

Both wrong numbers sit in the same sentence, and both times the exact answer above it is right.

Case one: 1/1000000000 prints an exact answer of 1/1000000000 and a preview of 0. Case two: 1000000000000000000000000000000 / 7 prints the exact answer as that fraction, and a preview of 142857142857142850000000000000. The true expansion is 142857142857142857142857142857 — the first 17 digits match, then the remaining 13 become zeros and the printed number is off by about seven trillion.

The cause is two lines. One converts the fraction back into a JavaScript double, the next formats that double to eight decimal places. Everything else in the file stays in BigInt. I went looking for a broken formula and found a broken formatter.

Zero and negative inputs are the cleanest part of the file.

Input Printed Correct
3/4 = 6/8 The ratios match. yes
0/4 = 0/9 The ratios match. yes
5/□ = 0/9 No solution. yes
0/□ = 5/9 No solution. yes
-3/4 = □/20 -15 yes
3/-4 = □/20 -15 yes
-3/-4 = □/20 15 yes
100 split 3 : 2 60 and 40 yes
-100 split 3 : 2 -60 and -40 yes
2 scaled from a base of -4 to 6 -3, factor -3/2 yes
2x + 3y = 6 slope -2/3, y = (-2/3)x + 2 yes
rise 1, run 12 8.33%, 4.76°, ratio 1 : 12 yes

When the blank can only be filled by a zero denominator, the page refuses: "No solution." When both sides of a cross product reduce to zero, it says so. It never divides by zero and never prints Infinity in a slot a student might copy down.

Every calculator I have tested before this one failed in exactly this band. This one fails somewhere else.

The input limit makes the precision unreachable.

math.mjs handles 30-digit integers exactly, and I verified that with the 10^30 case above. The input field refuses anything longer than 32 characters.

So the exactness is real, tested, and hard to reach on purpose. If you paste a number from a spreadsheet it probably carries a comma, and 1,000 is rejected too — the comma is not in the grammar. That's a smaller complaint than a wrong answer, but it's the same story: the engine is more capable than the box you type into.

What this test cannot tell you

The module only. I did not drive the browser UI, the SVG graph, the drag handles, or the copy button, so a bug in the wiring between page and module would not appear in any of the 77 rows.

I also did not look at traffic. I have no idea whether a real visitor ever lands on those two magnitudes, and I'm not going to invent a percentage. Two wrong numbers exist; how often they're seen is unmeasured.

And the eight-decimal rounding is not the bug. 1/7 previewing as 0.14285714 is correct, and the page says "up to 8 decimal places". The bug is narrower: the preview is computed from a float, so at the top and bottom of the range it stops being a rounding of the right number at all.

Disclosure

proportioncalculatoronline.com is ours — a free proportion calculator with worked steps. Same people, same test harness. We run these audits on our own sites because the alternative is hearing about it from a stranger on a forum.

What happens next

The fix is dull: compute the preview from the fraction instead of from a double, and print nothing when the honest answer is below the precision on offer. A blank line beats a confident zero.

If you find a case the 77 missed, check the answer column first. It has not failed me yet. The line under it is a different story.

Top comments (0)