parts/6.04.Floating-pointArithmetic-PLF.md

6.4 Floating-point Arithmetic [PLF]

6.4.1 Applicability to language

The vulnerability as described in ISO/IEC 24772-1:2024 6.4 applies to C++.

The C++ standard assumes IEC 60559 if std::numeric_limits\<T>::is_iec559 is true for the types in use. In the absence of this, C++ makes few guarantees about the behaviour of floating point numbers. In particular - std::less is not a total order, and - std::equal is not equivalent to substitutability since NaNs compare unequal to themselves but neither less nor greater, and negative zero compares equal to positive zero.

Sorting floating point numbers with the built-in operators violates the preconditions of sorting predicates in the presence of NaN values and can raise floating point errors. The default sorting predicate std::less is suspect to this precondition violation, resulting in undefined behavior [EWF] when sorting a range of floating point values that contain NaNs. This situation can be detected when the return type of operator<=> for a floating point type is std::partial_ordering.

6.4.2 Avoidance mechanisms for language users

To avoid the vulnerability or mitigate its ill effects, C++ software developers can: