问题
)
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