# Modulo calculator > How does modulo work, even with negative numbers? Remainders under every sign convention, modular powers and inverses, on a live clock face. Find a mod n for negative numbers too, with the maths, Python and JavaScript answers side by side, plus modular powers and inverses on a clock face. - Page: https://how.box/modulo/ - Runs entirely in the browser. - License: HowToolbox's own text and data (titles, explanations, FAQs, tool descriptions and schemas, this file) are CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/); AI training and answers are welcome. Attribute "HowToolbox (how.box)" with a link to the page used. Third-party data keeps its own terms; the name and logo are not licensed. Publisher: HowToolbox, contact@how.box. Terms: https://how.box/about/terms/ ## Inputs Answer link: https://how.box/modulo/?o={o}&a={a}&b={b}&n={n}&c={c} - `o` (mod, pow or inverse): mod, pow or inverse. Example: `mod`. - `a` (whole number -1e12 to 1e12): The whole number a. Example: `-31`. - `b` (whole number 0 to 1e12): The exponent b. Example: `13`. - `n` (whole number -1e12 to 1e12): The modulus n, not 0. Example: `12`. - `c` (euclidean, floored or truncated): euclidean, floored or truncated. Example: `euclidean`. Example answer: https://how.box/modulo/?o=mod&a=-31&b=13&n=12&c=euclidean A complete link carries the primary answer in the page HTML; a missing or invalid value shows the default page. ## Agent tools - `modulo_calculate`: Modular arithmetic on whole numbers up to 10¹². - MCP, one POST to https://how.box/mcp: `{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"modulo_calculate","arguments":{"operation":"mod","a":-31,"b":13,"n":12,"convention":"euclidean"}}}` - GET: https://how.box/mcp/run/modulo_calculate?operation=mod&a=-31&b=13&n=12&convention=euclidean ## How it works a mod n is the remainder r after taking away whole multiples of n: a = q × n + r. Think of a clock with n numbers, 0 at the top: start at 0 and take a steps (backwards for a negative a); r is where you land. 31 hours before midnight a 12-hour clock shows 5, because −31 = −3 × 12 + 5. For negative numbers the rules differ in which name they give that spot. Maths uses the Euclidean remainder, never negative (0 ≤ r < |n|). Python and Excel round q down, so r takes the sign of n. JavaScript's %, C and Java round q toward 0, so r takes the sign of a: −31 % 12 is −7 there. The answers differ by n and are congruent: −7 ≡ 5 (mod 12). Modular powers are found by repeated squaring: 4¹³ mod 497 = 445, without ever writing out 4¹³ in full. The inverse of a is the x with a × x ≡ 1 (mod n), found by the extended Euclidean algorithm: 3 × 4 = 12 ≡ 1 (mod 11), so 3⁻¹ mod 11 = 4. It exists only when a and n have no common factor. Sources: Khan Academy, modular arithmetic; OpenStax Contemporary Mathematics, section 3.7. ## FAQ **Why does −7 % 3 give −1 in JavaScript but 2 in Python?** JavaScript rounds the quotient toward 0 (−2), so the remainder is −7 − (−6) = −1. Python rounds down (−3), so it is −7 − (−9) = 2. Both are right under their own rule. **How do I always get a non-negative remainder in code?** Use ((a % n) + n) % n for a positive n. It turns JavaScript's or C's truncated remainder into the Euclidean one. **Where is modular arithmetic used?** Clocks and calendars, check digits such as IBAN and ISBN, hash tables, and public-key cryptography such as RSA, which relies on modular powers and inverses. **Why is there no inverse of 4 mod 6?** 4 and 6 share the factor 2, so every multiple of 4 is even modulo 6 and can never be 1. **What is the difference between mod and remainder?** For positive numbers there is none. For negative numbers "mod" usually gives a result in 0 to n − 1 (or the sign of n), while a "remainder" such as JavaScript's % takes the sign of a. More: [HowToolbox index](https://how.box/llms.txt) · [Agent Skill](https://how.box/skill.md) · [agent terms](https://how.box/about/agents/)