所以我经常看到这样的东西:
#define gf_PI f32(3.14159265358979323846264338327950288419716939937510)
#define gf_PIhalf f32(3.14159265358979323846264338327950288419716939937510 * 0.5)
这意味着我每次在代码中使用 gf_PIhalf 时都会计算一半的 PI 值,对吗?
直接写半个 PI 的值不是更好吗?
做以下事情不是更好吗:
#define gf_PI f32(3.14159265358979323846264338327950288419716939937510)
const float gf_PIHalf = gf_PI * 0.5f; // PIHalf is calculated once
最后,这样做不是最好的(以及为什么它似乎不是一种常见的做法):
const float gf_PI = 3.14159265358979323846264338327950288419716939937510;
const float gf_PIHalf = gf_PI * 0.5f;
最佳答案
This means that half PI value is calculated every time I use gf_PIhalf in my code, right?
不,不太可能。
您可以合理地指望您的编译器在编译时而不是运行时执行该乘法。
关于c++ - 定义常量时包括算术运算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15146454/