What's wrong with std::nan/std::nanf under Visual Studio?

前提是你 提交于 2019-12-23 04:27:09

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!