Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion ngraph/lib/demand.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from __future__ import annotations

import math
from dataclasses import dataclass, field
from typing import Optional, Tuple

from ngraph.lib.algorithms.base import MIN_FLOW
from ngraph.lib.flow_policy import FlowPolicy
from ngraph.lib.graph import NodeID, StrictMultiDiGraph

Expand All @@ -21,6 +23,16 @@ class Demand:
flow_policy: Optional[FlowPolicy] = None
placed_demand: float = field(default=0.0, init=False)

@staticmethod
def _round_float(value: float) -> float:
"""Round ``value`` to avoid tiny floating point drift."""
if math.isfinite(value):
rounded = round(value, 12)
Comment on lines +26 to +30
Copy link

Copilot AI May 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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)

Copilot uses AI. Check for mistakes.
if abs(rounded) < MIN_FLOW:
return 0.0
return rounded
return value

def __lt__(self, other: Demand) -> bool:
"""
Compare Demands by their demand_class (priority). A lower demand_class
Expand Down Expand Up @@ -94,7 +106,10 @@ def place(

# placed_now is the difference from the old placed_demand
placed_now = self.flow_policy.placed_demand - self.placed_demand
self.placed_demand = self.flow_policy.placed_demand
self.placed_demand = self._round_float(self.flow_policy.placed_demand)
remaining = to_place - placed_now

placed_now = self._round_float(placed_now)
remaining = self._round_float(remaining)

return placed_now, remaining