我不明白为什么我的程序做了这样的计算:奇怪的计算
#define PixelsPerMeter 40.0
// 40 pixels ~ 1 meter
#define Meters2Pixels(meters) (float)meters*PixelsPerMeter
#define Pixels2Meters(pixels) (float)pixels/PixelsPerMeter
// The speed of free falling
#define G Meters2Pixels(9.81)
// ...
float mHeight = 768;
float _windPower = Meters2Pixels(-5.0);
// ...
float x1 = (mHeight/G) * _windPower;
cout << "G: " << G << "; wind: " << _windPower << "\n";
cout << "Height: " << mHeight << "\n";
cout << "Calculating: " << mHeight/G * _windPower << "\n";
=>
G: 392.4; wind: -200
Height: 768
Calculating: -626300
我不明白为什么...如果我计算这个由手,举例来说,我得: -391.4
怎么了?
我还建议在括号内附上宏参数,以防您传入的值将您的运算符优先级更改为不合需要的值。 – mdec 2010-10-03 12:52:17
很好的答案,谢谢。是的,宏是邪恶的:) – Ockonal 2010-10-03 12:58:32
宏不是邪恶的,但我不认为这是一个很好的用例。 – alternative 2010-10-03 13:03:54