Skip to content

[Python] Fix modulo with negative numbers for bigint#4463

Merged
dbrattli merged 1 commit into
mainfrom
fix-python-bigint-modulo
Mar 29, 2026
Merged

[Python] Fix modulo with negative numbers for bigint#4463
dbrattli merged 1 commit into
mainfrom
fix-python-bigint-modulo

Conversation

@dbrattli

Copy link
Copy Markdown
Collaborator

Summary

  • Fix bigint modulo (%) with negative numbers returning Python floored remainder instead of .NET truncated remainder (fixes [Python] Modulo with negative numbers fails #4462)
  • -5I % 3I now correctly returns -2I instead of 1I
  • int and int64 were already correct thanks to Rust wrapper types whose __mod__ uses Rust's native % (truncated)
  • Added tests for negative modulo on int, int64, and bigint

Test plan

  • ./build.sh test python --skip-fable-library-core — all 2107 tests pass

🤖 Generated with Claude Code

Python's `%` operator uses floored remainder semantics while .NET uses
truncated remainder. For bigint, `op_modulus` was using Python's `%`
directly, so `-5I % 3I` returned `1` instead of `-2`. Fix by adjusting
the result when dividend and divisor have different signs.

int32 and int64 were already correct thanks to Rust wrapper types that
implement truncated remainder via Rust's native `%` operator.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown
Contributor

Python Type Checking Results (Pyright)

Metric Value
Total errors 18
Files with errors 4
Excluded files 4
New errors ✅ No
Excluded files with errors (4 files)

These files have known type errors and are excluded from CI. Remove from pyrightconfig.ci.json as errors are fixed.

File Errors Status
temp/tests/Python/test_applicative.py 12 Excluded
temp/tests/Python/test_hash_set.py 3 Excluded
temp/tests/Python/test_nested_and_recursive_pattern.py 2 Excluded
temp/tests/Python/fable_modules/thoth_json_python/encode.py 1 Excluded

@dbrattli dbrattli merged commit d8572aa into main Mar 29, 2026
24 checks passed
@dbrattli dbrattli deleted the fix-python-bigint-modulo branch March 29, 2026 14:50
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.

[Python] Modulo with negative numbers fails

1 participant