Skip to content

feat(stdlib): implement math.fmod for bitwise.lua bit32 verification#199

Merged
davydog187 merged 3 commits into
mainfrom
feat/math-fmod
May 7, 2026
Merged

feat(stdlib): implement math.fmod for bitwise.lua bit32 verification#199
davydog187 merged 3 commits into
mainfrom
feat/math-fmod

Conversation

@davydog187
Copy link
Copy Markdown
Contributor

Add math.fmod for bitwise.lua bit32 verification

Plan: .agents/plans/A5a-bitwise-suite-math-fmod.md

Goal

Implement math.fmod so bitwise.lua can finish its bit32-library
verification block (line 278 onward). This was the last remaining gap
discovered while shipping A5.

Success criteria

  • math.fmod(x, y) works per Lua 5.3 §6.7: "Returns the remainder
    of the division of x by y that rounds the quotient towards zero."
    Truncated-quotient remainder via Kernel.rem/2 for integers and
    :math.fmod/2 for floats.
  • math.fmod of two integers returns an integer; otherwise returns
    a float. Verified by math.fmod returns integer remainder and
    math.fmod with mixed int/float returns a float tests.
  • mix test passes, no regressions. 1394 → 1402 tests, 0 failures
    (8 new tests, 1 fewer skipped — bitwise.lua moved from
    @skipped_tests to @ready_tests).
  • Unit tests in test/lua/vm/stdlib/math_test.exs cover
    integer/integer, integer/float, float/float, negative dividend,
    mininteger / -1 overflow guard, integer divide-by-zero (raises
    "bad argument Add CI #2 ... (zero)"), float divide-by-zero (raises — see
    Discoveries), non-number argument errors, and missing-argument
    errors.
  • bitwise.lua passes end-to-end. Verified by running the file
    directly and by adding it to @ready_tests (mix test --only lua53 passes 5 ready files now).

Changes

 .agents/plans/A5a-bitwise-suite-math-fmod.md |  16 +++-
 lib/lua/vm/stdlib/math.ex                    |  69 +++++++++++++++++
 test/lua/vm/stdlib/math_test.exs             | 106 +++++++++++++++++++++++++++
 test/lua53_suite_test.exs                    |   2 +-
 4 files changed, 190 insertions(+), 3 deletions(-)

Discoveries

  • Lua 5.3 returns NaN for math.fmod(x, 0.0) when either argument is a
    float. The BEAM has no NaN value, and the rest of this VM raises on
    zero-divisor float arithmetic (see safe_divide in
    lib/lua/vm/executor.ex:1690). For consistency we raise bad argument #2 to 'math.fmod' (zero) for both integer and float zero divisors.
    Documented inline in lib/lua/vm/stdlib/math.ex.
  • Integer math.fmod(mininteger, -1) is short-circuited to 0 to avoid
    an overflow trap (matches the C implementation in lmathlib.c's
    special case for d == -1).
  • Moved bitwise.lua from @skipped_tests to @ready_tests in
    test/lua53_suite_test.exs, locking in the suite-count gain.
  • No other math.* gaps surfaced while wiring fmod in. math.modf and
    math.atan2 are not exercised by bitwise.lua.

Verification

mix format
mix compile --warnings-as-errors
mix test                                 # 1402 tests, 0 failures, 31 skipped
mix test test/lua/vm/stdlib/math_test.exs  # 25 tests, 0 failures
mix run --no-mix-exs -e 'Code.require_file("test/support/lua_test_case.ex"); Lua.TestCase.run_lua_file("test/lua53_tests/bitwise.lua")'
# → testing bitwise operations / + / testing bitwise library / + / + / OK
mix test --only lua53                    # 29 tests, 0 failures, 24 skipped

Out of scope (intentional)

  • Other missing math.* functions (math.modf, math.atan2, etc.)
    unless also exercised by bitwise.lua. None surfaced.
  • A package.searchers table — separate concern from the package.preload
    hook added in A5.

davydog187 added 3 commits May 6, 2026 17:52
Adds `math.fmod(x, y)` per Lua 5.3 §6.7 — truncated-quotient remainder.
Returns an integer when both args are integers, otherwise a float
computed via :math.fmod/2. Integer zero divisor raises "bad argument #2
... (zero)"; mininteger / -1 short-circuits to 0 to match the C
implementation's overflow guard.

Unblocks bitwise.lua's bit32.lshift verification block (line 278), so
the file now passes end-to-end. Moved from @skipped_tests to
@ready_tests.

Plan: A5a
@davydog187 davydog187 merged commit 50f6644 into main May 7, 2026
4 checks passed
@davydog187 davydog187 deleted the feat/math-fmod branch May 7, 2026 01:07
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.

1 participant