r/cpp • • Jul 29 '26

C++26: Reducing undefined behaviour

https://www.sandordargo.com/blog/2026/07/29/cpp26-reduces-undefined-behaviour
89 Upvotes

48 comments sorted by

View all comments

Show parent comment

4

u/eisenwave WG21 Member Jul 30 '26

Something else that isn't common knowledge that would be great to take out is floating point divisions by zero being UB. A lot of (if not most!) people think that this is guaranteed to produce a NaN, to the point where I'm not sure that there are compilers that exploit this behaviour. It'd be much better as implementation defined behaviour at this point (at least, on systems that are ieee), because its what everyone expects

I think you mean that people assume it prodoces infinity. At least dividing a finite number by zero gives you infinities in practice.

Specifying that in the standard is quite tricky because any statement made about floating-point types needs to apply universally to every possible floating-point format, and there are lots of those floating around.

The solution is most likely going to be very similar to C's Annex F, where implementations optionally adhere to the behavior for ISO/IEC 60559 types there, and if they do, you get the division by zero you expect. The non-Annex-F case is probably going to be implementation-defined at best, but UB is surprisingly appropriate for it.

3

u/TheThiefMaster C++latest fanatic (and game dev) Jul 30 '26

and there are lots of those floating around.

Are there? There were historically, but isn't IEEE floating point universal now?

PS: nice pun.

2

u/serviscope_minor Aug 06 '26

Well, there's IEEE and IEEE. The hardware generally mostly implements IEEE with not that many bugs (the 80bit FPUs of x87 are fading into the past). Buuuut you might not get quite the same results on different compilers and platforms due to some optimizations. I have heard a case for double/double1394 or fastdouble/double types giving respectively the best the compiler can do and a pedantically correct same-on-all-platforms but potentially slower calculation.