Visual Studio下的std :: nan/std :: nanf有什么问题? [英] What's wrong with std::nan/std::nanf under Visual Studio?
问题描述
想与<a href="https://en.cppreference.com/w/cpp/numeric/math/nan" rel="nofollow noreferrer"> std :: nan 和 std :: nanf 来创建带有一些自定义有效负载的nan值(非装箱)
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:
但是,对于Visual Studio 2015,该功能显然未正确实现. cppreference.com提出的确切样本产生:
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)
这不是我们期望的. VS实现错误吗?如果不是,那么用于生成7ff8000000000001
和7ff8000000000002
的正确参数是什么?
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
?
推荐答案
std::nan
根据定义具有实现定义的行为,因此VS只要返回某种安静的NaN肯定是没有错的.
std::nan
has, by definition, implementation-defined behavior, so VS is certainly not wrong as long as it returns some kind of quiet NaN.
实际上,Microsoft文档有这样的说法:
In fact, the Microsoft docs have this to say:
nan
函数返回一个浮点值,该值对应于安静的(非信令)NaN. 输入值将被忽略.有关如何用NaN表示输出的信息,请参见printf,_printf_l,wprintf,_wprintf_l.
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/zh-CN/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
这篇关于Visual Studio下的std :: nan/std :: nanf有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!