简体   繁体   English

编译时与运行时常量

[英]Compile-time vs runtime constants

I'm currently developing my own math lib to improve my c++ skills. 我正在开发自己的数学库来提高我的c ++技能。 I stumbled over boost's constants header file and I'm asking myself what is the point of using compile-time constants over runtime declared constants? 我偶然发现了boost的常量头文件 ,我问自己在运行时声明的常量上使用编译时常量有什么意义?

const float root_two = 1.414213562373095048801688724209698078e+00;
const float root_two = std::sqrt( 2.0f );

Isn't there an error introduced when using the fixed compile-time constant but calculations while running the application with functions? 使用固定的编译时常量但是在运行带有函数的应用程序时进行计算时是不是会引入错误? Wouldn't then the error be negleted if you use runtime constants? 如果使用运行时常量,那么错误是否会被忽略?

  1. As HansPassant said, it may save you a micro-Watt. 正如HansPassant所说,它可能会为你节省微瓦。 However, note that the compiler will sometimes optimize that away by evaluating the expression during compilation and substituting in the literal value. 但是,请注意,编译器有时会通过在编译期间评估表达式并在文字值中替换来优化它。 See this answer to my earlier question about this. 请参阅我之前关于此问题的答案

  2. Isn't there an error introduced when using the fixed compile-time constant? 使用固定的编译时常量时是否存在错误? If you are using arbitrary-precision data types, perhaps. 如果您正在使用任意精度数据类型。 But it is more efficient to use plain data types like double and these are limited to about 16 decimal digits of precision anyways. 但是使用像double这样的普通数据类型更有效,并且这些数据仅限于大约16个十进制数字的精度。

  3. Based on (2), your second initialization would not be more precise than your first one. 基于(2),您的第二次初始化将不会比您的第一次初始化更精确。 In fact, if you precomputed the value of the square root with an arbitrary precision calculator, the literal may even be more precise. 实际上,如果使用任意精度计算器预先计算平方根的值,则文字甚至可能精确。

A library such as Boost must work in tons of environments. 像Boost这样的库必须在大量的环境中工作。 The application that uses the library could have set FPU could be in flush-to-zero mode, giving you 0.0 for denormalized (tiny) results. 使用该库的应用程序可以设置FPU可以处于从零到零模式,为非正规化(微小)结果提供0.0。

Or the application could have been compiled with the -fast-math flag, giving inaccurate results. 或者应用程序可能已使用-fast-math标志进行编译,从而产生不准确的结果。

Furthermore, a runtime computation of (a + b + c) depends on how the compiler generated code will store intermediate results. 此外,(a + b + c)的运行时计算取决于编译器生成的代码将如何存储中间结果。 It might chose to pop (a + b) from the FPU as a 64-bit double, or it could leave it on the FPU stack as 80 bits. 它可能选择从FPU弹出(a + b)为64位双精度,或者它可以将它作为80位留在FPU堆栈上。 It depends on tons of things, also algebraic rewrites of associativity. 它取决于大量的东西,也取决于相关性的代数重写。

All in all, if you mix different processors, operating systems, compilers and the different applications the library is used inside, you will get a different result for each permutation of the above. 总而言之,如果您混合使用不同的处理器,操作系统,编译器以及库内部使用的不同应用程序,您将获得上述每种排列的不同结果。

In some (rare) siturations this is not wanted; 在一些(罕见的)情境中,这不是必需的; you can be in need for an exact constant value. 你可能需要一个确切的恒定值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM