Way*_*int 0 c floating-point floating-point-conversion
我从命令行输入int d.现在我面临这个问题:
float a,b;
int d;
float piece;
printf("Please enter the parts to divide the interval: ");
scanf("%d", &d);
a=0;
b=1;
piece=b-a/(float)d;
printf("%f\n",piece);
Run Code Online (Sandbox Code Playgroud)
我想要的是printf一些依赖于&d的浮点数.例如,当我在这里写5时,我会得到0.20000,对于6 - 0,166666,但我仍然得到所有数字的1.000000,有人知道解决方案吗?
小智 6
除法优先于减法,因此需要将减法放在括号内.您不必显式地将d转换为float; 将浮子除以它将促使浮子浮动.
piece = (b - a) / d;
Run Code Online (Sandbox Code Playgroud)