Skip to content

fix: handle empty power samples in total_power() to prevent ZeroDivisionError#1143

Closed
wangwangbobo wants to merge 1 commit intomlco2:masterfrom
wangwangbobo:fix-total-power-zero-division-v2
Closed

fix: handle empty power samples in total_power() to prevent ZeroDivisionError#1143
wangwangbobo wants to merge 1 commit intomlco2:masterfrom
wangwangbobo:fix-total-power-zero-division-v2

Conversation

@wangwangbobo
Copy link
Copy Markdown

Description

This PR fixes a potential ZeroDivisionError in the CPU.total_power() method when power history is empty.

Changes

Code Changes

  • 3 lines modified in codecarbon/external/hardware.py

Testing

  • The fix ensures that when no power samples are collected, the method returns 0.0 W instead of raising ZeroDivisionError

…ionError

- Check power_history_in_W after conversion instead of before
- Return 0.0 W with warning when no samples collected
- Fixes potential division by zero when power history is empty
@wangwangbobo wangwangbobo requested a review from a team as a code owner March 26, 2026 12:44
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.

2 participants