Why is this simple piece of code not working?

2019-03-06 01:56发布

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.

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;

5条回答
霸刀☆藐视天下
2楼-- · 2019-03-06 02:25

Use

fbb=(double)bb/1000;

bb is integer and result is integer, and then converted to double

查看更多
放我归山
3楼-- · 2019-03-06 02:29

bb is int. So bb/1000 is doing a int division, which results again in an int = 1000 (no decimals). That int value is cast to a double.

查看更多
叼着烟拽天下
4楼-- · 2019-03-06 02:30

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
查看更多
做自己的国王
5楼-- · 2019-03-06 02:31

When you perform

 fbb = bb/1000;

It treats operation as int/int and returns an int

Try

fbb = ((double)bb)/1000.000;

It will be treated as (double)/(double) and return a double.

查看更多
三岁会撩人
6楼-- · 2019-03-06 02:36

When you perform

fbb = bb/1000;

It treats operation as int/int and returns an int. its demotion of value.

Also take long bb; instead of int as int has value 32767 as its high value.

Try

fbb = bb/1000.000;

or

fbb = (double)bb/1000;
查看更多
登录 后发表回答