【发布时间】:2011-10-07 07:21:22
【问题描述】:
我对此感到非常困惑...这是我的代码的摘录..
float m = 0.0, c = 0.0;
printf("toprightx = %d bottomrightx = %d toprighty = %d bottomrighty = %d\n",
toprightx, bottomrightx, toprighty, bottomrighty);
// find m and c for symmetry line
if (toprightx == bottomrightx) {
m = (-toprighty + bottomrighty);
}
else {
m = (-toprighty + bottomrighty) / (toprightx - bottomrightx);
}
c = -toprighty - (m * toprightx);
printf("m = %f and c = %f\n", m, c);
这是输出:
toprightx = 241 bottomrightx = 279 toprighty = 174 bottomrighty = 321
m = -3.000000 and c = 549.000000
为什么输出是四舍五入 m 和 c?我已将它们声明为浮点数,所以我不明白为什么代码返回整数。 m的正确值应该是-3.8684。
(请注意,toprightx、bottomrightx、toprighty、bottomrighty 已在代码中进一步声明为整数。)
【问题讨论】:
-
我不是 C 程序员,但我很确定它首先进行整数数学运算(由于使用了变量)并将结果填充到浮点数中。您需要将其他变量定义为浮点数。
-
为什么'toprightx,bottomrightx'会有所不同?如果 'toprightx' 少,不应该是 'topleftx' [或只是 'left']?
-
@PaulPRO:你为什么要提供赏金?你在这里找什么?
标签: c++ types floating-point integer