小编Mat*_*ton的帖子

返回float变量会失去C中的精度

嗨是的我在使用以下代码时遇到了一些麻烦.上面的函数执行并输出132.87到float变量但是当我将它返回到主程序时,输出则缩短为132.00

这显然是我想念的一些简单的事可以帮助我吗?非常感激.

calcSigmaXY(max) {
 int count= 0;
 float runTotal = 0, product[max];
 for (count = 1; count <= max; count++) {
     product[count] = pointData[count][1] * pointData[count][2];
     runTotal = runTotal + product[count];
 }
 printf("\nruntotal is %4.2f",runTotal); // outputs 132.87
 return(runTotal);
} 

int maxPoints = 6;
float sigmaXY = 0,
sigmaXY = calcSigmaXY(maxPoints);
printf("\nsigmaxy set to : %4.2f\n", sigmaXY); // outputs 132.00
Run Code Online (Sandbox Code Playgroud)

c floating-point return function

4
推荐指数
1
解决办法
112
查看次数

标签 统计

c ×1

floating-point ×1

function ×1

return ×1