'Behavior of std::complex on undefined results
On page 1052 of (the openly accessible draft of) the C++17 standard, it says that calling a function which has a mathematically undefined result is undefined behavior. For example,
#include <iostream>
#include <complex>
int main() {
std::cout << std::complex<double>(0., 0.) / std::complex<double>(0., 0.);
return 0;
}
prints (-nan,-nan) on clang 7.
Float division by zero is undefined behavior in general, but implementations may follow IEEE-754 semantics (Annex F in the C standard, and mentioned in passing in some of the C++ standard, but I'm not sure on that point). So float division by zero is defined to be NaN in implementations in which std::numeric_limits<double>::is_iec559 is true.
This is a language lawyer question, so I'm not sure why the comments are going down the path of "undefined behavior can do anything". Of course, but is it a defect in the standard that the results are not specified (or considered implementation-defined) for implementations that follow IEEE-754?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
