Add mod, a remainder that is never negative - #54
Merged
Stiven-Gjekaj merged 2 commits intoAug 24, 2026
Merged
Conversation
% 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>
3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
mod(n, d), a floored remainder builtin.%keeps truncating toward zero (section 5 / stability);modis the wrapping form every ring buffer and clock face wants.Behaviour
mod(-1, 10)9mod(-7, 3)2mod(7, 3)1(same as%)mod(1, -10)-9(sign follows divisor)mod(1, 0)modulo by zero(same words as%)a - d * floor(a/d)).Touches
src/builtins.rs— register, implement, unit tests,BUILTIN_NAMES, kind countstests/golden.rs— pinned cases for negatives, floats, zero divisorTests
cargo test --workspacegreen (MSVC)cargo clippy --all-targets -- -D warningsgreencargo fmt --all --checkgreenFixes #44