问题
Wanted to play with std::nan and std::nanf to create nan values with some custom payload (non boxing).
However, it really does not work as expected:
However, with Visual Studio 2015, the function apparently is not implemented right. The exact sample proposed by cppreference.com produces:
nan("1") = nan (7ff8000000000000)
nan("2") = nan (7ff8000000000000)
Which is not what we expect. Is VS implementation wrong? If not, what would be the right arguments to use to produce 7ff8000000000001
and 7ff8000000000002
?
回答1:
std::nan
has, by definition, implementation-defined behavior, so VS is certainly not wrong as long as it returns some kind of quiet NaN.
In fact, the Microsoft docs have this to say:
The
nan
functions return a floating-point value that corresponds to a quiet (non-signalling) NaN. The input value is ignored. For information about how a NaN is represented for output, see printf, _printf_l, wprintf, _wprintf_l.
https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/nan-nanf-nanl?view=vs-2017
This appears to be true (unsurprisingly), based on a small test:
std::nan("1") = 7ff8000000000000
std::nan("2") = 7ff8000000000000
std::nan("NAN(1)") = 7ff8000000000000
std::nan("NAN(2)") = 7ff8000000000000
std::nan("NAN1") = 7ff8000000000000
std::nan("NAN2") = 7ff8000000000000
std::nan("NAN 1") = 7ff8000000000000
std::nan("NAN 2") = 7ff8000000000000
来源:https://stackoverflow.com/questions/53811296/whats-wrong-with-stdnan-stdnanf-under-visual-studio