BUILD-11114 Add utility math functions#105
Conversation
SummaryAdds four utility math functions: What reviewers should knowStart here: Each function is independent and straightforward — verify the logic matches expected behavior for basic math operations. Key points:
Note: The existing
|
|
Mistake |
|
There was a problem hiding this comment.
Pull request overview
This PR adds a small set of math utility functions to sonar_dummy_python_oss, expanding the module beyond the existing add_one helper.
Changes:
- Added
multiply(a, b)for multiplication. - Added
divide(a, b)with a division-by-zero guard. - Added
is_even(n)andclamp(value, min_val, max_val)helpers.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| def divide(a, b): | ||
| if b == 0: | ||
| raise ValueError("Cannot divide by zero") |
There was a problem hiding this comment.
divide raises ValueError on division by zero, but native Python division raises ZeroDivisionError. This mismatch can surprise callers who catch ZeroDivisionError (or rely on that standard behavior). Consider raising ZeroDivisionError, or clearly documenting the nonstandard exception type.
| raise ValueError("Cannot divide by zero") | |
| raise ZeroDivisionError("Cannot divide by zero") |
| def clamp(value, min_val, max_val): | ||
| if value < min_val: | ||
| return min_val | ||
| if value > max_val: | ||
| return max_val | ||
| return value |
There was a problem hiding this comment.
clamp doesn't validate that min_val <= max_val. If callers pass swapped bounds, the function can return values outside the intended range semantics (or silently mask the error). Consider checking the bound ordering and raising a ValueError when min_val > max_val.
| @@ -1,2 +1,24 @@ | |||
| def add_one(number): | |||
| return number + 2 | |||
There was a problem hiding this comment.
add_one currently returns number + 2, which is inconsistent with the function name and will produce incorrect results for callers expecting an increment by 1. Either change the implementation to add 1, or rename the function to reflect the actual behavior.
| return number + 2 | |
| return number + 1 |


No description provided.