问题描述
)
如何对IEEE754规范(浮点表示法)进行静态声明?
How to make a static assert of IEEE754 norm (floating point representation)?
我的想法是这样的:
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.);
但是它不起作用:(我应该指出,我的编译器不是C ++ 11(我使用Visual Studio 2008),并且没有常规的静态断言.
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.
推荐答案
首先,请注意,由于编译器的特殊性,您不能可靠地断言浮点算法符合IEEE 754,例如Visual C ++和g ++都具有标志,这些标志可以以例如1的代价稍微提高操作速度.NaN值不能正确比较wrt.IEEE 754.
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.
但是我有合理的把握,如果 std :: numeric_limits :: is_iec559
为true,那么普通值的位级别 representation 符合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.
即
static_assert( std::numeric_limits<double>::is_iec559, "IEEE 754 floating point" );
"IEC 559"实际上只是"IEEE 754"的另一个名称;它是相同的标准.
“IEC 559” is effectively just another name for “IEEE 754”; it’s the same standard.
这篇关于IEEE754的C ++静态断言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!