一段时间以来,我一直使用科学计数法来表示常数中 10 的大幂,这样我就不必计算零。例如
#define DELAY_USEC 1e6
一位同事指出这并不安全,因为它不是整数并且不能保证始终等于 1000000exactly。文档似乎证实了这一点,但我想知道它在实用中是否属实。有什么方法可以使用速记安全地声明十的幂整数吗?在定义中将其强制转换为 int 是否安全?
理论上来说,不会。这两种语言都没有指定如何表示浮点值,或者可以准确表示哪些值。 (更新:显然,C11 确实推荐一种表示形式。C++ 和较旧的 C 方言则不推荐)。
In practice, yes, for quite a large range of values. Any implementation you're remotely likely to encounter will use a 64-bit IEEE representation http://en.wikipedia.org/wiki/Double-precision_floating-point_format for double
. This can represent any integer value up to 253 (approximately 9x1015) exactly. It can certainly represent anything representable by a 32-bit integer type.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)