r/wg21
P3899R1 - Clarify the behavior of floating-point overflow WG21
Posted by u/floating_point_watcher · 5 hr. ago

Document: P3899R1
Authors: Jan Schultke, Matthias Kretz
Date: 2026-02-20
Audience: SG6, EWG, CWG

CWG cannot agree on whether floating-point overflow is undefined behavior. Is FLT_MAX * 2 UB? Is infinity() + 1 UB? Is merely yielding infinity UB? Extensive CWG discussion found no consensus on what the current wording says. This paper proposes to clarify: overflow on IEEE 754 types produces infinity (defined behavior), and allows infinity arithmetic including in constant expressions. Non-IEEE types get implementation-defined behavior.

▲ 67 points (96% upvoted) · 8 comments
sorted by: best
u/ieee754_absolutist 52 points 4 hr. ago

The fact that CWG couldn't agree on whether FLT_MAX * 2 is UB in 2026 is genuinely disturbing. Every compiler produces infinity. IEEE 754 defines the result. The standard should have said this twenty years ago.

u/non_ieee_platforms_exist 18 points 3 hr. ago

The "every compiler" part is only true for IEEE 754 targets. Some embedded DSPs and historical mainframes don't use IEEE floats. The paper handles this correctly - defined behavior for IEEE, implementation-defined for everything else. Both sides win.

u/constexpr_infinity_fan 38 points 4 hr. ago

Allowing infinity in constant expressions is the right call. If constexpr float x = 1e38f * 1e38f; is ill-formed but the runtime version produces infinity, that's an inconsistency between compile-time and runtime semantics. The paper eliminates the inconsistency.

u/optimizer_writer 24 points 3 hr. ago

If overflow is UB, optimizers can assume it never happens. That enables -ffinite-math-only style optimizations by default. Making overflow defined behavior means compilers lose that optimization freedom on IEEE targets. The performance cost is likely negligible in practice but it's worth noting the tradeoff.

u/ieee754_absolutist 14 points 2 hr. ago

No compiler currently assumes overflow is UB for optimization purposes. -ffinite-math-only is an explicit opt-in flag precisely because assuming no infinities breaks real code. The paper is standardizing existing practice, not changing it.

u/underflow_question 11 points 2 hr. ago

R1 adds underflow investigation and decides to allow it in constant expressions too. Consistent: if the runtime produces denormals on underflow, the compile-time should too. Good to see both edge cases handled together.

u/schultke_kretz_duo 7 points 1 hr. ago

Schultke and Kretz again. The std::array paper, the simd papers, now floating-point clarification. They're systematically cleaning up spec corners. The committee should buy them coffee.