C++ static assert of IEEE754

烂漫一生 提交于 2021-01-28 06:37:40

问题


)

How to make a static assert of IEEE754 norm (floating point representation)?

My idea was something like that:

static unsigned char c[8] = { 0, 0, 0, 0, 0, 0xd0, 0x84, 0x40 };
static double d= *reinterpret_cast<double *>(c);
BOOST_STATIC_ASSERT(d==666.);

But it doesnt't work :( I should point out that my compiler is not C++11 (I use visual studio 2008) and I don't have regular static asserts.


回答1:


First, note that due to compiler idiosyncrasies you can't reliably assert that floating point arithmetic conforms to IEEE 754, e.g. both Visual C++ and g++ have flags that yield marginally faster operations at the cost of e.g. NaN values not comparing correctly wrt. IEEE 754.

But I'm reasonably sure that if std::numeric_limits::is_iec559 is true, then the bit level representation of ordinary values conforms to IEEE 754.

I.e.

static_assert( std::numeric_limits<double>::is_iec559, "IEEE 754 floating point" );

“IEC 559” is effectively just another name for “IEEE 754”; it’s the same standard.



来源:https://stackoverflow.com/questions/22785934/c-static-assert-of-ieee754

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