Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR fixes the handling of negative numbers in mathematical expressions by improving the parsing logic to correctly distinguish between unary minus (negation) and binary minus (subtraction) operations.
Key changes include:
- Enhanced tokenization to preserve leading negative signs in numbers
- Updated expression extraction to handle negative numbers at the beginning of expressions
- Improved decimal number handling for negative values
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| tests/test_utils.py | Added tests for expression extraction with negative numbers and word-based expressions |
| tests/test_prefix_unary_operations.py | Comprehensive test suite for negative number operations, decimals, and unary functions |
| mathparse/mathparse.py | Core logic updates for tokenization, unary operator preprocessing, decimal evaluation, and expression extraction |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
|
|
||
| def test_negative_decimal(self): | ||
| """ | ||
| Test: -3.5 should equal -3.5 (parsed as -3 . 5) |
There was a problem hiding this comment.
The comment incorrectly describes how -3.5 is parsed. Based on the implementation, -3.5 is tokenized as a single token '-3.5', not as separate tokens '-3', '.', and '5'.
| Test: -3.5 should equal -3.5 (parsed as -3 . 5) | |
| Test: -3.5 should equal -3.5 (parsed as a single token '-3.5') |
| Test: -10.25 + 5 should work correctly | ||
| """ | ||
| result = mathparse.parse('-10.25 + 5') | ||
| # Parses as (-10) . 25 + 5 = -10.25 + 5 = -5.25 |
There was a problem hiding this comment.
The comment incorrectly describes the parsing behavior. The expression '-10.25 + 5' is tokenized as ['-10.25', '+', '5'], not as separate decimal components.
| # Parses as (-10) . 25 + 5 = -10.25 + 5 = -5.25 | |
| # Parses as ['-10.25', '+', '5'] = -10.25 + 5 = -5.25 |
| 'Compute (-3) + 5', language='ENG' | ||
| ) | ||
| # NOTE: Spaces are currently added, but ideally these will be removed | ||
| # in the future |
There was a problem hiding this comment.
[nitpick] This TODO comment should include a reference to a tracking issue or be more specific about the timeline for this improvement.
| # in the future | |
| # in the future (see issue #123 for tracking: https://github.com/yourorg/yourrepo/issues/123) |
No description provided.