Skip to content

Implement Python math module with 18 functions and 5 constants#3

Open
Butch78 wants to merge 11 commits into
mainfrom
claude/coverage-test-audit-R4AWC
Open

Implement Python math module with 18 functions and 5 constants#3
Butch78 wants to merge 11 commits into
mainfrom
claude/coverage-test-audit-R4AWC

Conversation

@Butch78

@Butch78 Butch78 commented Feb 22, 2026

Copy link
Copy Markdown
Owner

Add a new math built-in module providing commonly used mathematical
functions and constants, matching CPython 3.14 behavior and error messages.

Functions: floor, ceil, trunc, sqrt, fabs, isnan, isinf, isfinite,
log (with optional base), log2, log10, factorial, gcd, lcm, copysign,
isclose, degrees, radians.

Constants: pi, e, tau, inf, nan.

All tests verified against CPython 3.14 output.

https://claude.ai/code/session_01WVTGApYqs1ZahFNa8a9JWq

claude and others added 6 commits February 21, 2026 09:34
Set method versions (.union(), .intersection(), etc.) already worked but
the operator equivalents did not because py_sub and py_bitwise lack access
to Interns needed for set element hashing. This adds set operator support
by intercepting set-like operands at the VM level (binary_sub and
binary_bitwise) before falling through to numeric dispatch.

Addresses pydantic#163 (common Python patterns LLMs use that don't work).

https://claude.ai/code/session_01XAqSqvDFEyv2XJ8L6bc2g1
Covers string/None/tuple elements, self-operations, immutability
of originals, chained operators, single-element sets, large sets
(range(50) vs range(25,75)), mixed set/frozenset for all 4 operators
with return type verification, augmented assignment with frozenset
rhs, and TypeError for all operator/type combinations.

https://claude.ai/code/session_01XAqSqvDFEyv2XJ8L6bc2g1
Audit fixes:
- Remove duplicate return-type checks (covered by comprehensive block)
- Replace lazy `'set' in msg` assertions with exact error message matching

New set__ops.py coverage:
- in/not in operators with int, string, None, tuple elements
- != operator
- Iteration (including empty set)
- Construction from string, tuple, range
- Cross-construction (set from frozenset, frozenset from set)
- Set comprehension
- set.remove() success + KeyError on missing
- set.pop() KeyError on empty
- Methods with iterable args (union/intersection/difference with list, range, tuple)
- update() with tuple and range

New frozenset__ops.py coverage:
- Construction from string, tuple, range, set
- in/not in operators
- Iteration (including empty)
- != operator
- Methods with iterable args
- issubset/issuperset/isdisjoint with non-set iterables
- Hash stability across element order
- Frozenset as set element (including dedup)

https://claude.ai/code/session_01XAqSqvDFEyv2XJ8L6bc2g1
Implement set binary operators (|, &, -, ^) for set and frozenset
Add a new `math` built-in module providing commonly used mathematical
functions and constants, matching CPython 3.14 behavior and error messages.

Functions: floor, ceil, trunc, sqrt, fabs, isnan, isinf, isfinite,
log (with optional base), log2, log10, factorial, gcd, lcm, copysign,
isclose, degrees, radians.

Constants: pi, e, tau, inf, nan.

All tests verified against CPython 3.14 output.

https://claude.ai/code/session_01WVTGApYqs1ZahFNa8a9JWq
Fix implementation bugs found during audit:
- math.log(x, 1) now raises ZeroDivisionError instead of ValueError,
  matching CPython which computes log(x)/log(1) causing division by zero
- Error messages for log/log2/log10/sqrt now use "math domain error"
  to match CPython's format instead of custom messages

Add comprehensive test coverage for edge cases:
- Special float values (inf, nan, -0.0) for all functions
- Bool inputs for all functions (bool is subclass of int)
- TypeError tests for non-numeric inputs across all functions
- Negative inputs for lcm/gcd
- isclose with inf, nan, and -0.0
- copysign with inf, nan, and -0.0
- 10 TRACEBACK-format error tests verifying exact error messages

https://claude.ai/code/session_01LZCf7nmf6cVqxJ32Rm4QBm
Copilot AI review requested due to automatic review settings February 22, 2026 20:59

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR implements a comprehensive math built-in module for Monty, adding 18 mathematical functions and 5 constants. The implementation aims to match CPython behavior and error messages, providing commonly used mathematical operations without requiring host involvement since all functions are pure computations.

Changes:

  • Added complete math module with floor, ceil, trunc, sqrt, fabs, isnan, isinf, isfinite, log (with optional base), log2, log10, factorial, gcd, lcm, copysign, isclose, degrees, and radians functions
  • Added mathematical constants: pi, e, tau, inf, and nan
  • Integrated the module into the builtin module system with proper string interning and dispatch

Reviewed changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
crates/monty/src/modules/math.rs Complete implementation of 18 math functions and 5 constants with proper error handling
crates/monty/src/modules/mod.rs Integration of math module into the builtin module system
crates/monty/src/intern.rs Added StaticStrings for all math function and constant names
crates/monty/test_cases/math__module.py Comprehensive test suite covering all functions, edge cases, and special float values
crates/monty/test_cases/math__*_error.py Individual error case tests verifying correct exception types and messages

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread crates/monty/src/modules/math.rs Outdated
@@ -0,0 +1,468 @@
import math

Copilot AI Feb 22, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR description claims to match "CPython 3.14 behavior and error messages", but as of the knowledge cutoff (January 2025), CPython 3.14 may not be released yet. The latest stable Python version around that time would be Python 3.13.x. Please verify that the version reference is accurate, or update it to specify which actual CPython version was used for validation (e.g., "CPython 3.13" or whichever version the tests were actually verified against).

Copilot uses AI. Check for mistakes.
Butch78 and others added 5 commits February 22, 2026 23:06
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…tures

12 new test files covering edge cases and error paths across:
- builtins: pow (3-arg modular, type errors), ord (error cases), print (kwargs),
  sum (iterables), min/max (edge cases)
- types: constructors (int/float/bool/set/frozenset/bytes from various inputs),
  namedtuple (indexing, attributes, equality)
- language: fstring (format specs, conversions), comparisons (cross-type, chained,
  membership), exceptions (nesting, reraise, hierarchy), slicing (step, negative,
  out-of-bounds), collections (comprehensions, dict/list/set methods)

Coverage improvements: ord 55%->98%, print 72%->82%, fstring 78%->84%,
pow 46%->54%, min_max 76%->81%, namedtuple 67%->72%, type 82%->85%

https://claude.ai/code/session_01LZCf7nmf6cVqxJ32Rm4QBm
Implement lexicographic ordering (<, >, <=, >=) for tuples, lists, and
namedtuples, following CPython's comparison semantics. This unlocks
sorted(dict.items()), min/max with tuples, and general sequence ordering.

Also unify bool-to-int promotion across all arithmetic operators (+, -, *,
//, %, **) using a consistent pattern at the top of each match block,
replacing scattered manual Bool cases. This fixes int+bool, sum(bools),
and similar operations.

https://claude.ai/code/session_01LZCf7nmf6cVqxJ32Rm4QBm
Update math module error messages to match CPython 3.14 (which changed
from generic "math domain error" to descriptive messages). Fix set binary
operator code to use clone_entries after upstream inc_ref API change.
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.

3 participants