我碰巧遇到了一些类似的编程风格,主要是当有一个浮点或双操作时。
ratio = 1.0 * (top - bottom) / (right - left);  所涉及的所有变量都是浮动的。
让1.0与结果相乘有什么意义?
按照我的想法,乘以1.0是一些额外的burden.Since,结果不会改变。
或者编写具有and  1==1的条件是否类似。
P.S:有些情况下,在某些变量(比率除外)中,将非浮点数/双值赋值为长值或整数。
发布于 2011-12-16 10:32:49
在C++中,经验法则是
如果操作涉及浮动类型,则将两个操作数转换为浮动类型(结果为浮动类型)。
请记住,operation与operator非常相关。操作顺序由运算符优先确定。
C++中基本操作的优先级是非常自然的。数学:
*,/发生在+,-之前(中的表达式,)首先发生所以如果你有
float f = 1.0f + 1 / 2;
// then `f` will be `1.0f`, because
int   sub = 1 / 2     ;  // <- an integer division, happens first and gives 0
float f   = 1.0f + sub;  // <- 0 because the division result was evaluated first最后的结果是float类型,因为最后一个操作是一个float + int。
另一个例子,涉及带括号的表达式:
float f = (1.0f + 1) / 4;
// `f` will be `0.5` this time. The braced expressions happens first:
float sub = 1.0f + 1;  // float + int = float
float f   = sub / 4;   // float / int = float这里需要注意的是,4到4.0f的转换在操作之前发生,就像在这个示例性的程序集中:
// [mnemomic] [what]+ [target]
fload 1.0f, float_register_0
fload 1,    float_register_1
fadd  float_register_0, float_register_1, float_register_2 // sub is in [2]
fload 4,    float_register_3                               // 4 is in [3]
fdiv  float_register_2, float_register_3, f                // [2]/[3] -> f记住
如果操作涉及浮动类型,则将两个操作数转换为浮动类型(结果为浮动类型)。
https://stackoverflow.com/questions/8532461
复制相似问题