Skip to content

Commit b12b32c

Browse files
committed
fix(sonar): drop float-equality boundary checks in _percentile (S1244)
Iteration 4: the previous max/min clamp introduced two S1244 BUGs ('Do not perform equality checks with floating point values') at the if clamped_q == 0.0 and == 1.0 boundary returns. Removing those guards entirely: the linear-interpolation path below already collapses to sorted_values[lo] when lo == hi, which is exactly what happens at clamped_q == 0 (pos = 0) and clamped_q == 1 (pos = n-1). Same semantics, no float-equality compares, no flow-analysis flags. Verified locally: q ∈ {-1, 0, 0.5, 1, 2}, empty list, single-element list all return the expected values.
1 parent 3af1762 commit b12b32c

1 file changed

Lines changed: 3 additions & 6 deletions

File tree

benchmarks/cell_metrics.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,10 @@ def _percentile(sorted_values: Sequence[float], q: float) -> float:
8686
n = len(sorted_values)
8787
if n == 0:
8888
return 0.0
89-
# Clamp q to [0, 1] explicitly so the boundary returns below are
90-
# always reachable and Sonar's S2583 flow analysis doesn't fire.
89+
# Clamp q to [0, 1]; the interpolation below naturally returns the
90+
# first / last element when clamped_q is at the boundary (pos becomes
91+
# 0 or n-1, so lo == hi and we short-circuit to sorted_values[lo]).
9192
clamped_q = max(0.0, min(1.0, q))
92-
if clamped_q == 0.0:
93-
return float(sorted_values[0])
94-
if clamped_q == 1.0:
95-
return float(sorted_values[-1])
9693
pos = clamped_q * (n - 1)
9794
lo = math.floor(pos)
9895
hi = math.ceil(pos)

0 commit comments

Comments
 (0)