为什么在 C 中将 1,000,000,000 写为 1000*1000*1000? [英] Why write 1,000,000,000 as 1000*1000*1000 in C?
问题描述
在 Apple 创建的代码中,有这样一行:
In code created by Apple, there is this line:
CMTimeMakeWithSeconds( newDurationSeconds, 1000*1000*1000 )
是否有任何理由将1,000,000,000
表示为1000*1000*1000
?
Is there any reason to express 1,000,000,000
as 1000*1000*1000
?
为什么不用 1000^3
呢?
推荐答案
以乘法方式声明常量的一个原因是为了提高可读性,同时不影响运行时性能.此外,表明作者正在以乘法的方式思考数字.
One reason to declare constants in a multiplicative way is to improve readability, while the run-time performance is not affected. Also, to indicate that the writer was thinking in a multiplicative manner about the number.
考虑一下:
double memoryBytes = 1024 * 1024 * 1024;
显然比:
double memoryBytes = 1073741824;
因为后者乍一看不是 1024 的三次方.
as the latter doesn't look, at first glance, the third power of 1024.
正如 Amin Negm-Awad 提到的,^
运算符是二进制 XOR
.许多语言缺少内置的编译时求幂运算符,因此缺少乘法.
As Amin Negm-Awad mentioned, the ^
operator is the binary XOR
. Many languages lack the built-in, compile-time exponentiation operator, hence the multiplication.
这篇关于为什么在 C 中将 1,000,000,000 写为 1000*1000*1000?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!