JavaScript traps · Chapter 13 of 16

Number precision past 2^53

Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Time
  • 3 tasks
  • JavaScript
  • In the all labs plan

The first 2 chapters of JavaScript traps are free. This one comes with the all labs plan.

Why you need this

JSON.parse turns digits into a number as it reads them, and rounding is not an error. Here is a loyalty card number, 19 digits:

const text = '{"card":4000123412341234567,"name":"Imran"}';
const { card } = JSON.parse(text);
console.log(card);
console.log(String(card), BigInt(card));
4000123412341234700
4000123412341234700 4000123412341234688n

The digits are gone the moment the text is parsed. String prints the shortest digits that round to the same number, and BigInt gives that number's exact value: neither can bring back digits the number never held. A BigInt cannot come straight out of JSON either (case 8): JSON has only one kind of number.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    repro.js: give order 9007199254740993 the wrong id on purpose

    Write reproduce() in repro.js. Build the JSON text the partner sends, with one order whose id is 9007199254740993, written as digits in the text (a JSON number, not a string), for example {"orders":[{"id":9007199254740993,"outlet":"Indiranagar","total":105}]}. Call parseOrders(body) from partner.js with it, and resolve to the id of the order it gives back.

    The check runs your reproduce() twice: beside the original partner.js it must resolve to '9007199254740992' (someone else's order), and beside a fixed partner.js to '9007199254740993'. So it must really call parseOrders with that id. Write the id inside the text: as a number in your own code it would be rounded before parseOrders saw it. reproduce() must finish within 2 seconds.

  2. Task 2

    partner.js: every id arrives with every digit

    Fix parseOrders(body) in partner.js so every order's id is kept exactly, digit for digit, as a string: an id of 9007199254740993 in the JSON becomes '9007199254740993', and a small id like 81234 becomes '81234'. Every other field stays as it came: totals, quantities and prices stay numbers, strings stay strings, true and null stay true and null. Keep the log line partner order <id>: <outlet>, ₹<total>, one per order, as it is.

    The check uses its own JSON, with ids of up to 19 digits, two neighbouring ids above 2^53, and spaces and new lines in the text. Turning the id into a string or a BigInt after JSON.parse is too late: the number is rounded while it is parsed.

  3. Task 3

    partner.test.js: a test that would have caught it

    Write a vitest test file, partner.test.js, that would have caught this bug before the partner went live. The check runs your file three times: beside the original partner.js, where at least one test must fail; beside the fixed partner.js, where every test must pass; and beside a wrong fix that turns the id into a BigInt after JSON.parse, where at least one test must fail.

    So parse JSON text with an id that is not a safe integer (above 9007199254740991, such as 9007199254740993) and expect every digit back as a string. Write the ids inside the JSON text: a number literal that big in the test file is rounded too. Each test must finish within 3 seconds.

What you will learn

  • repro.js: give order 9007199254740993 the wrong id on purpose
  • partner.js: every id arrives with every digit
  • partner.test.js: a test that would have caught it

More in Time

Build "Number precision past 2^53" yourself

Tapri runs on JavaScript, and Kabir, its senior developer, keeps assigning you tickets. Each is a real trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of JavaScript traps are free. This one comes with the all labs plan.