站点介绍

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.
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.
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.
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.
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.
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.
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/
评论列表 (0条)