Merged
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR enhances demand placement by introducing deterministic rounding to mitigate floating-point drift.
- Added a
_round_floathelper to snap tiny values to zero. - Applied rounding to
placed_demand,placed_now, andremaininginplace(). - Imported
mathandMIN_FLOWto support the new logic.
Comments suppressed due to low confidence (2)
ngraph/lib/demand.py:110
remainingis computed using the unroundedplaced_now, which can reintroduce floating-point drift. Consider roundingplaced_nowfirst and then calculatingremainingbased on the rounded value.
remaining = to_place - placed_now
ngraph/lib/demand.py:27
- New rounding behavior in
_round_floatshould have dedicated unit tests covering finite values, non-finite values, and theMIN_FLOWthreshold to ensure correctness.
def _round_float(value: float) -> float:
Comment on lines
+26
to
+30
| @staticmethod | ||
| def _round_float(value: float) -> float: | ||
| """Round ``value`` to avoid tiny floating point drift.""" | ||
| if math.isfinite(value): | ||
| rounded = round(value, 12) |
There was a problem hiding this comment.
[nitpick] The precision value 12 is a magic number. Extract this into a named constant (e.g., ROUND_PRECISION) or make it configurable to improve clarity and maintainability.
Suggested change
| @staticmethod | |
| def _round_float(value: float) -> float: | |
| """Round ``value`` to avoid tiny floating point drift.""" | |
| if math.isfinite(value): | |
| rounded = round(value, 12) | |
| # Precision for rounding floating-point values | |
| ROUND_PRECISION: int = 12 | |
| @staticmethod | |
| def _round_float(value: float) -> float: | |
| """Round ``value`` to avoid tiny floating point drift.""" | |
| if math.isfinite(value): | |
| rounded = round(value, Demand.ROUND_PRECISION) |
networmix
added a commit
that referenced
this pull request
Jun 2, 2025
networmix
added a commit
that referenced
this pull request
Jun 2, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Testing
pytest --cov=ngraph --cov-fail-under=85 --cov-report term-missing