Skip to content

Add mod, a remainder that is never negative - #54

Merged
Stiven-Gjekaj merged 2 commits into
Stiven-Gjekaj:mainfrom
VedantMadane:feat/issue-44-mod
Aug 24, 2026
Merged

Stiven-Gjekaj merged 2 commits into
Stiven-Gjekaj:mainfrom
VedantMadane:feat/issue-44-mod

Conversation

@VedantMadane

Copy link
Copy Markdown
Contributor

Summary

Adds mod(n, d), a floored remainder builtin. % keeps truncating toward zero (section 5 / stability); mod is the wrapping form every ring buffer and clock face wants.

Behaviour

Call Result
mod(-1, 10) 9
mod(-7, 3) 2
mod(7, 3) 1 (same as %)
mod(1, -10) -9 (sign follows divisor)
mod(1, 0) error modulo by zero (same words as %)
  • Integers stay integers; any float promotes, matching other binary numeric builtins.
  • Implementation adjusts the truncated remainder when operands have different signs (equivalent to a - d * floor(a/d)).

Touches

  • src/builtins.rs — register, implement, unit tests, BUILTIN_NAMES, kind counts
  • tests/golden.rs — pinned cases for negatives, floats, zero divisor
  • Spec section 8.7, wiki math section, changelog, grammar, README counts

Tests

  • cargo test --workspace green (MSVC)
  • cargo clippy --all-targets -- -D warnings green
  • cargo fmt --all --check green

Fixes #44

% truncates toward zero, so -7 % 3 is -1. That stays put under the
stability guarantee. Wrapping uses (ring buffers, clocks, board edges)
want the floored form instead. mod(n, d) is that form: the sign follows
the divisor, a zero divisor refuses with the same words % uses, and
floats match because % already accepts them.

Fixes Stiven-Gjekaj#44

Signed-off-by: Vedant Madane <6527493+VedantMadane@users.noreply.github.com>
Record the builtin in section 8.7, bump the pinned counts, and note the
wrapping cases next to the other math functions.

Signed-off-by: Vedant Madane <6527493+VedantMadane@users.noreply.github.com>
@Stiven-Gjekaj
Stiven-Gjekaj merged commit 67b50af into Stiven-Gjekaj:main Aug 24, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add mod, a remainder that is never negative

2 participants