首页 通用效率工具内容详情

Modulo Calculator: What Is -7 mod 3, Mod vs Remainder Settled - Both results side by side, negatives at a glance

2026-11-17 3 访客
Modulo Calculator: What Is -7 mod 3, Mod vs Remainder Settled

站点名称:Modulo Calculator: What Is -7 mod 3, Mod vs Remainder Settled

所属分类:通用效率工具

官方网址:https://www.9314.cn/tools/modulo-calc/en/

SEO查询: 爱站网 站长工具

进入网站

站点介绍

Modulo Calculator: What Is -7 mod 3, Mod vs Remainder Settled

A LeetCode problem cost me two hours once: the ring-buffer index (i-1) % n yields -1 when i=0, which is undefined in JS rather than the last element. That was the day it truly landed: the same (-7) % 3 gives 2 in Python and -1 in JavaScript - both correct, different conventions.

The short version

  • Both results side by side: floored modulo (quotient toward negative infinity, remainder shares the modulus sign) next to truncated remainder (quotient toward zero, remainder shares the dividend sign) - -7 mod 3 = 2, -7 rem 3 = -1, the divergence visible at a glance.
  • All four negative combinations: -7 mod 3, 7 mod -3, -7 mod -3, 7 mod 3, each with its quotient and decomposition.
  • Honest edges: mod 0 errors on the spot (mathematically undefined); past 2^53-1 a precision warning appears - computed anyway, flagged as approximate.
  • Cheat sheet: congruence, addition and multiplication distributive laws, the fold-negative formula ((a%n)+n)%n, clock arithmetic - everything ring indices, hash bucketing and fast exponentiation lean on.
  • Local computation, works offline, nothing uploaded.

Why two answers exist

Both conventions satisfy "dividend = quotient x modulus + remainder"; they differ only in how the quotient is taken: truncation cuts -7/3 to quotient -2, remainder -1; flooring takes it to -3, remainder 2. Math texts and number theory use modulo; C, Java and JavaScript % give the remainder, Python % the modulo. Crossing languages makes the difference a real trap.

When it bites

Ring buffers, hash bucketing and periodic scheduling break on negative remainders; in fast exponentiation (a^b) mod p, one unfixed negative in the middle leaves the answer off by p. After my incident the habit stuck: ring indices are always ((i % n) + n) % n - one line that fixes every language's negative remainder.

FAQ

What is the difference between modulo and remainder?

For positive numbers they agree; the difference shows with negatives. Truncated remainder rounds the quotient toward zero: -7 divided by 3 gives quotient -2, remainder -1. Floored modulo rounds toward negative infinity (or picks a non-negative remainder): -7 mod 3 = 2. Math textbooks and number theory use modulo; C, Java and JavaScript % give the remainder, while Python % gives the modulo.

Why does -7 mod 3 equal 2?

Modulo requires the remainder to share the sign of the modulus (a positive modulus means a non-negative remainder): -7 = (-3) x 3 + 2, quotient -3 rounded toward negative infinity, remainder 2 inside [0,3). Under truncation, -7 = (-2) x 3 + (-1), quotient -2 rounded toward zero, remainder -1 sharing the dividend's sign. Both are self-consistent - they are just different conventions.

What happens when the modulus is 0?

You get an error. Dividing by zero is undefined and so is mod 0; the page tells you the modulus cannot be 0. That is not a crash - it is math refusing to cooperate.

Are my numbers uploaded anywhere?

No. The computation happens in this device's browser - there is no upload endpoint and the page works offline. Recent history lives in this browser's localStorage; Clear my data in the footer removes it in one click.

中文版:https://www.9314.cn/tools/modulo-calc/

 暂无评论,快来抢沙发吧~

发布评论