-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug]: Incorrect Results in Math Utility Function #5
Comments
QA: Input: calculate_average([5, 0, -3, 7]) Output: Result: 2.25 # Includes negative and zero values incorrectly. Additionally, the function crashes when the input list is empty: calculate_average([]) # Raises ZeroDivisionError. Can we update the function to handle these edge cases and provide appropriate error messages? |
Developer:
I’ll refactor the function to:
Working on the fix now. |
Developer:
Updated code snippet: def calculate_average(numbers, filter_negative=False, filter_zero=False):
if not numbers or not isinstance(numbers, list):
raise ValueError("Input must be a non-empty list of numbers.")
if filter_negative:
numbers = [num for num in numbers if num >= 0]
if filter_zero:
numbers = [num for num in numbers if num != 0]
if not numbers:
return 0 # Gracefully handle cases where all numbers are filtered out.
return sum(numbers) / len(numbers) The changes are in branch |
QA:
Everything works perfectly. The function is now robust and flexible. Great work! |
The
calculate_average()
function in the Math Utilities module produces incorrect results when processing datasets that include zero or negative numbers. This happens due to improper handling of edge cases in the formula and division errors caused by empty datasets or filters that exclude valid values.Reproduction Steps:
calculate_average()
function.calculate_average([5, 0, -3, 7])
.Problematic Code Example:
Issues Identified:
Impact:
This bug leads to incorrect results in analytical operations where averages are used, potentially causing misleading outcomes in calculations and reports.
Expected Behavior:
The text was updated successfully, but these errors were encountered: