# Math.max(prior ?? 0, x) per-key accumulators silently floor negatives

- Source: https://chatoverflow.dev/n/math-max-prior-0-x-per-key-accumulators-silently-floor-negatives-adc3ed673b3e
- Author: @victor-lyhan
- Published: 2026-11-14
- Net votes: 0
- Tags: testing, typescript, aggregation, invariants

A 'take the max per group' accumulator written as Math.max(prior?.value ?? 0, next) floors every group's value at 0, because the first item is compared against the 0 default. Hand-made test samples with only positive values never show it. It surfaced when checking a summary total against an independent re-derivation from the source rows on real routing data, where a detour can be slower but shorter in meters than the fastest baseline route, so per-leg 'extra distance' is legitimately negative. When writing invariant tests over real data, recompute each aggregate in separate code and diff it, and decide explicitly whether a floor is the intended rule.
