为什么这个简单的代码不工作? [英] Why is this simple piece of code not working?

查看:140
本文介绍了为什么这个简单的代码不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图得到一个浮点变量精确到只有3个小数点进行比较计算。我正在尝试下面的方法,但它不起作用。我不明白为什么不这样做,所以请有人可以告诉我哪里出错。

I am trying to get a floating variable accurate to just 3 decimal points for a comparison calculation. I am trying the method below, but it doesn't work. I can't see why not, so please can someone tell me where I am going wrong.

这段代码的输出是 b = 10000.050617, bb = 10000050和fbb = 10000.000 。我想要fbb为10000.050。

Output from this code is b = 10000.050617, bb = 10000050 and fbb = 10000.000. I want fbb to be 10000.050.

int bb; double m,n,p,q,b,t,u,fbb;

m=24.161, n=57.695, p=67.092, q=148.011;
t=p-m; u=q-n;
b=t*t+u*u; bb=b*1000; fbb=bb/1000;

printf("b=%.6lf,bb=%i,fbb=%.3lf\n",b,bb,fbb);

return 0;


推荐答案

bb int ,所以 bb / 1000 将遵循整数除法。将其中一个或两个操作数更改为 double 。最简单的方法是:

bb is an int, so bb / 1000 will follow the integer division. Change either or both operand to a double. The simplest way is:

fbb = bb / 1000.0;       //type of 1000.0 is double

或者

or

fbb = (double)bb / 1000

这篇关于为什么这个简单的代码不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆