{"course":{"slug":"prompting","title":"Getting Real Work out of a Model","url":"https://addaly.com/learn/prompting","level":"beginner","track":"start"},"lesson":{"slug":"arithmetic-and-code","title":"Arithmetic it should not do in its head","url":"https://addaly.com/learn/prompting/arithmetic-and-code","position":39,"of":72,"minutes":9,"answer":"A model generates digits rather than calculating, so ask it to write and run code or produce a spreadsheet formula whenever the number matters.","keyIdea":"A model generates digits rather than calculating, so ask it to write and run code or produce a spreadsheet formula whenever the number matters, and check the inputs and the row count rather than the answer."},"body":{"format":"markdown","text":"## Why the sums go wrong\n\nA model produces digits the way it produces words: one token at a time, each chosen because it is a likely continuation. There is no arithmetic unit. On small, familiar sums this works, because the patterns are all over the training data. On long multiplication, percentages of awkward numbers, date arithmetic across months, or a column of forty figures, it drifts.\n\nThe failure is quiet. `4,182 × 37` produces a plausible six-digit number that is wrong in the middle. Nothing about the output looks different from a correct one.\n\nAdd the tokenisation problem from module one (long numbers are split into digit groups in ways that vary between models) and you have a system that is fluent about quantities and unreliable with them.\n\n## The fix: make it write code, not answers\n\nMost current chat products can execute code. ChatGPT, Claude, Gemini and Copilot all offer some form of it, usually Python. Ask for the calculation to be done that way:\n\n> Do not calculate this in prose. Write Python that computes it, run it, and show me both the code and the result.\n\nNow the arithmetic is performed by an interpreter that does not guess, and you get something you can read:\n\n```python\nrows = [(\"Mar\", 41820, 0.18), (\"Apr\", 38945, 0.18), (\"May\", 52310, 0.12)]\ntotal = sum(amount * (1 + vat) for _, amount, vat in rows)\nprint(round(total, 2))\n```\n\nThree things improved at once. The number is computed rather than generated. The code shows which figures and which rate were used, so a wrong assumption is visible. And you can rerun it next month by changing three lines.\n\n> Figure: Wrong totals per hundred sums, by method. Bars (wrong per 100): In prose, ten rows 3; In prose, forty rows 18; In code, forty rows 0; In code, but inputs misread 2. Code removes the arithmetic error, which rose steeply with the number of rows. It leaves the extraction error untouched, which is why printing the parsed rows matters more than checking the total.\n\n## Where this matters most\n\n**Anything cumulative.** Totals, running balances, compound interest, percentage change across periods. Errors here compound in the ordinary sense as well as the mathematical one.\n\n**Anything with dates.** \"Ninety working days from 14 March, excluding Kenyan public holidays\" is a calculation, not a fact, and models are unreliable at it in prose.\n\n**Anything over more than about ten rows.** Summing a short column in prose is usually fine. Summing forty is not, and the error rate rises exactly where checking gets harder.\n\n**Anything a spreadsheet would normally do.** Grouping, sorting by two keys, deduplicating, joining two lists on a shared column.\n\n## Checking code you cannot write\n\nYou do not need to write Python to use this. You need to read four things:\n\n1. **The input numbers.** Do they match your source? This catches most errors.\n2. **The operation.** Multiplication where you expected addition, a rate of 0.18 where the invoice says 18%.\n3. **The row count.** `len(rows)` should equal what you gave it. A quiet drop of three rows is the most common silent failure.\n4. **The result's plausibility.** Roughly what you expected? An order of magnitude out is the sign of a unit error.\n\nAsk for those to be printed: \"print the row count and the first two rows before the total\". Thirty seconds of checking, and it catches the class of error that a bare number never reveals.\n\n## The free path\n\nIf your tool cannot run code, or the data should not leave your machine, you can do this offline. Ask for the Python and run it yourself: Python is free, installed on most Macs and Linux machines, and a small download on Windows. LibreOffice Calc, also free, will do the same work with formulas, and asking for the formula rather than the answer is the same trick in a spreadsheet.\n\n> Give me the LibreOffice/Excel formula for this, not the answer. Explain what each part refers to.\n\nA formula sits in the sheet and recalculates. A number pasted from a chat is a fossil that nobody can audit next quarter.\n\n## The rule\n\n**Ask for the method, not the number**, whenever the number matters. A computed result you can rerun beats a generated result you have to trust, and the difference costs you one sentence in the prompt.\n\n## What code execution does not fix\n\nTwo things, and both catch people who have just discovered this technique.\n\n**Wrong inputs.** If the model misread `41,820` as `4,182` while pulling figures out of your pasted invoice, the code computes a perfect total of the wrong numbers. Extraction and calculation are separate steps with separate failure modes, which is exactly why printing the parsed rows matters more than checking the total.\n\n**Wrong logic.** Code that applies VAT to a line that was already inclusive is correct code and a wrong answer. Reading the operation is not optional; it is the check.\n\nThe gain here is real and specific: it removes arithmetic error, which was one class of failure among several. It does not turn an unverified answer into a verified one."},"check":{"question":"A bookkeeper asks a model to total 40 invoice amounts pasted as text. It returns a figure that looks about right but is out by 12,000. What is the most reliable prevention?","options":[{"text":"Asking it to work step by step, adding the numbers in groups of ten.","correct":false,"tempting":"Improves the odds somewhat while still generating digits rather than computing them, and adds many more places to slip."},{"text":"Asking it to write and run code that sums the values, and printing the row count and the parsed figures alongside the total.","correct":true},{"text":"Pasting the invoices in a cleaner format so the figures are easier to read.","correct":false,"tempting":"Helps with extraction errors but does nothing about arithmetic that is produced token by token."},{"text":"Running the same prompt three times and taking the total that appears twice.","correct":false,"tempting":"Measures stability; a systematic parsing or arithmetic error reproduces across runs."}]},"prev":{"slug":"when-steps-help","title":"Which tasks get better with steps, and which do not","url":"https://addaly.com/learn/prompting/when-steps-help"},"next":{"slug":"reasoning-models","title":"Models that think before answering","url":"https://addaly.com/learn/prompting/reasoning-models"},"authorship":"Founder and series editor: Dr Mustafa Mun. Written by AI models to a written standard. Exam keys checked blind by a second model: 3,665 of 3,666 agreed. Academic review invited.","review":{"status":"Draft, open for academic review"},"cite":"Addaly (2026) 'Arithmetic it should not do in its head', in Getting Real Work out of a Model. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/prompting/arithmetic-and-code (Accessed: 13 October 2026).","licence":{"name":"CC BY-NC-SA 4.0","url":"https://creativecommons.org/licenses/by-nc-sa/4.0/"},"reader":{"schema":1,"title":"Arithmetic it should not do in its head","courseTitle":"Getting Real Work out of a Model","module":{"index":5,"slug":"making-it-work-the-problem","title":"Making it work the problem"},"answer":null,"keyIdea":"A model generates digits rather than calculating, so ask it to write and run code or produce a spreadsheet formula whenever the number matters, and check the inputs and the row count rather than the answer.","hero":{"kind":"figure","figure":{"t":"figure","label":"Figure","figure":{"type":"bars","unit":"wrong per 100","title":"Wrong totals per hundred sums, by method","series":[{"label":"In prose, ten rows","value":3},{"label":"In prose, forty rows","value":18},{"label":"In code, forty rows","value":0,"highlight":true},{"label":"In code, but inputs misread","value":2}],"caption":"Code removes the arithmetic error, which rose steeply with the number of rows. It leaves the extraction error untouched, which is why printing the parsed rows matters more than checking the total."},"alt":"Figure: Wrong totals per hundred sums, by method. Bars (wrong per 100): In prose, ten rows 3; In prose, forty rows 18; In code, forty rows 0; In code, but inputs misread 2. Code removes the arithmetic error, which rose steeply with the number of rows. It leaves the extraction error untouched, which is why printing the parsed rows matters more than checking the total."}},"blocks":[{"t":"h2","id":"why-the-sums-go-wrong","runs":[{"text":"Why the sums go wrong"}]},{"t":"p","runs":[{"text":"A model produces digits the way it produces words: one token at a time, each chosen because it is a likely continuation. There is no arithmetic unit. On small, familiar sums this works, because the patterns are all over the training data. On long multiplication, percentages of awkward numbers, date arithmetic across months, or a column of forty figures, it drifts."}]},{"t":"p","runs":[{"text":"The failure is quiet. "},{"text":"4,182 × 37","code":true},{"text":" produces a plausible six-digit number that is wrong in the middle. Nothing about the output looks different from a correct one."}]},{"t":"p","runs":[{"text":"Add the tokenisation problem from module one (long numbers are split into digit groups in ways that vary between models) and you have a system that is fluent about quantities and unreliable with them."}]},{"t":"h2","id":"the-fix-make-it-write-code-not-answers","runs":[{"text":"The fix: make it write code, not answers"}]},{"t":"p","runs":[{"text":"Most current chat products can execute code. ChatGPT, Claude, Gemini and Copilot all offer some form of it, usually Python. Ask for the calculation to be done that way:"}]},{"t":"quote","runs":[{"text":"Do not calculate this in prose. Write Python that computes it, run it, and show me both the code and the result."}]},{"t":"p","runs":[{"text":"Now the arithmetic is performed by an interpreter that does not guess, and you get something you can read:"}]},{"t":"code","lang":"python","label":"Python","text":"rows = [(\"Mar\", 41820, 0.18), (\"Apr\", 38945, 0.18), (\"May\", 52310, 0.12)]\ntotal = sum(amount * (1 + vat) for _, amount, vat in rows)\nprint(round(total, 2))"},{"t":"p","runs":[{"text":"Three things improved at once. The number is computed rather than generated. The code shows which figures and which rate were used, so a wrong assumption is visible. And you can rerun it next month by changing three lines."}]},{"t":"h2","id":"where-this-matters-most","runs":[{"text":"Where this matters most"}]},{"t":"p","runs":[{"text":"Anything cumulative.","b":true},{"text":" Totals, running balances, compound interest, percentage change across periods. Errors here compound in the ordinary sense as well as the mathematical one."}]},{"t":"p","runs":[{"text":"Anything with dates.","b":true},{"text":" “Ninety working days from 14 March, excluding Kenyan public holidays” is a calculation, not a fact, and models are unreliable at it in prose."}]},{"t":"p","runs":[{"text":"Anything over more than about ten rows.","b":true},{"text":" Summing a short column in prose is usually fine. Summing forty is not, and the error rate rises exactly where checking gets harder."}]},{"t":"p","runs":[{"text":"Anything a spreadsheet would normally do.","b":true},{"text":" Grouping, sorting by two keys, deduplicating, joining two lists on a shared column."}]},{"t":"h2","id":"checking-code-you-cannot-write","runs":[{"text":"Checking code you cannot write"}]},{"t":"p","runs":[{"text":"You do not need to write Python to use this. You need to read four things:"}]},{"t":"ol","start":1,"items":[[{"text":"The input numbers.","b":true},{"text":" Do they match your source? This catches most errors."}],[{"text":"The operation.","b":true},{"text":" Multiplication where you expected addition, a rate of 0.18 where the invoice says 18%."}],[{"text":"The row count.","b":true},{"text":" "},{"text":"len(rows)","code":true},{"text":" should equal what you gave it. A quiet drop of three rows is the most common silent failure."}],[{"text":"The result’s plausibility.","b":true},{"text":" Roughly what you expected? An order of magnitude out is the sign of a unit error."}]]},{"t":"p","runs":[{"text":"Ask for those to be printed: “print the row count and the first two rows before the total”. Thirty seconds of checking, and it catches the class of error that a bare number never reveals."}]},{"t":"h2","id":"the-free-path","runs":[{"text":"The free path"}],"role":"free"},{"t":"p","runs":[{"text":"If your tool cannot run code, or the data should not leave your machine, you can do this offline. Ask for the Python and run it yourself: Python is free, installed on most Macs and Linux machines, and a small download on Windows. LibreOffice Calc, also free, will do the same work with formulas, and asking for the formula rather than the answer is the same trick in a spreadsheet."}]},{"t":"quote","runs":[{"text":"Give me the LibreOffice/Excel formula for this, not the answer. Explain what each part refers to."}]},{"t":"p","runs":[{"text":"A formula sits in the sheet and recalculates. A number pasted from a chat is a fossil that nobody can audit next quarter."}]},{"t":"h2","id":"the-rule","runs":[{"text":"The rule"}]},{"t":"p","runs":[{"text":"Ask for the method, not the number","b":true},{"text":", whenever the number matters. A computed result you can rerun beats a generated result you have to trust, and the difference costs you one sentence in the prompt."}]},{"t":"h2","id":"what-code-execution-does-not-fix","runs":[{"text":"What code execution does not fix"}]},{"t":"p","runs":[{"text":"Two things, and both catch people who have just discovered this technique."}]},{"t":"p","runs":[{"text":"Wrong inputs.","b":true},{"text":" If the model misread "},{"text":"41,820","code":true},{"text":" as "},{"text":"4,182","code":true},{"text":" while pulling figures out of your pasted invoice, the code computes a perfect total of the wrong numbers. Extraction and calculation are separate steps with separate failure modes, which is exactly why printing the parsed rows matters more than checking the total."}]},{"t":"p","runs":[{"text":"Wrong logic.","b":true},{"text":" Code that applies VAT to a line that was already inclusive is correct code and a wrong answer. Reading the operation is not optional; it is the check."}]},{"t":"p","runs":[{"text":"The gain here is real and specific: it removes arithmetic error, which was one class of failure among several. It does not turn an unverified answer into a verified one."}]}],"check":{"question":[{"text":"A bookkeeper asks a model to total 40 invoice amounts pasted as text. It returns a figure that looks about right but is out by 12,000. What is the most reliable prevention?"}],"options":[{"text":[{"text":"Asking it to work step by step, adding the numbers in groups of ten."}],"correct":false,"tempting":[{"text":"Improves the odds somewhat while still generating digits rather than computing them, and adds many more places to slip."}]},{"text":[{"text":"Asking it to write and run code that sums the values, and printing the row count and the parsed figures alongside the total."}],"correct":true,"tempting":null},{"text":[{"text":"Pasting the invoices in a cleaner format so the figures are easier to read."}],"correct":false,"tempting":[{"text":"Helps with extraction errors but does nothing about arithmetic that is produced token by token."}]},{"text":[{"text":"Running the same prompt three times and taking the total that appears twice."}],"correct":false,"tempting":[{"text":"Measures stability; a systematic parsing or arithmetic error reproduces across runs."}]}],"order":[2,1,0,3],"why":null},"prev":{"slug":"when-steps-help","title":"Which tasks get better with steps, and which do not","minutes":8},"next":{"slug":"reasoning-models","title":"Models that think before answering","minutes":9}}}