简体   繁体   English

为什么这个除法导致零?

[英]Why does this division result in zero?

I was writing this code in C when I encountered the following problem. 当我遇到以下问题时,我在C中编写此代码。

#include <stdio.h>
int main()
{
   int i=2;
   int j=3;
   int k,l;
   float a,b;
   k=i/j*j;
   l=j/i*i;
   a=i/j*j;
   b=j/i*i;
   printf("%d %d %f %f\n",k,l,a,b);
   return 0;
}

Can anyone tell me why the code is returning zero for the first and third variables ( k and a )? 谁能告诉我为什么代码为第一个和第三个变量( ka )返回零?

Are you asking why k and a show up as zero? 你问为什么k和a显示为零? This is because in integer division 2/3 = 0 (the fractional part is truncated). 这是因为在整数除法中2/3 = 0(小数部分被截断)。

What I think you are experiencing is integer arithmetic . 我认为你正在经历的是整数运算 You correctly suppose l and b to be 2, but incorrectly assume that k and a will be 3 because it's the same operation. 你正确地假设lb为2,但错误地认为ka将是3,因为它是相同的操作。 But it's not, it's integer arithmetic (rather than floating-point arithmetic). 但事实并非如此,它是整数运算(而不是浮点运算)。 So when you do i / j (please consider using some whitespace), 2 / 3 = 0.33333... which is cast to an int and thus becomes 0. Then we multiply by 3 again, and 0 * 3 = 0. 因此,当你做i / j (请考虑使用一些空格),2/3 = 0.33333 ... 被转换为int并因此变为0.然后我们再次乘以3,并且0 * 3 = 0。

If you change i and j to be float s (or pepper your math with (float) casts), this will do what you expect. 如果你将ij改为float (或用(float)强制转换你的数学运算),这将做你期望的。

You haven't said what you're getting or what you expect, but in this case it's probably easy to guess. 你还没有说出你得到的或你期望的东西,但在这种情况下,它可能很容易猜到。 When you do 'a=i/j*j', you're expecting the result to be roughly .2222 (ie 2/9), but instead you're getting 0.0. 当你做'a = i / j * j'时,你期望结果大致为.2222(即2/9),但你得到0.0。 This is because i and j are both int's, so the multiplication and (crucially) division are done in integer math, yielding 0. You assign the result to a float, so that 0 is then converted to 0.0f. 这是因为i和j都是int,所以乘法和(关键)除法是在整数数学中完成的,产生0.你将结果赋值给float,然后将0转换为0.0f。

To fix it, convert at least one operand to floating point BEFORE the division: a = (float)i/j*j); 要修复它,在除法之前将至少一个操作数转换为浮点: a = (float)i/j*j);

this is due to how the c compiler treats int in divisions: 这是由于c编译器如何处理div中的int:

 #include <stdio.h>
int main()
{
int i=2;
int j=3;
int k,l;
float a,b;
k=i/j*j; // k = (2/3)*3=0*3=0
l=j/i*i; // l = (3/2)*2=1*2=2
a=i/j*j; // same as k
b=j/i*i; // same as b
printf("%d %d %f %f/n",k,l,a,b);
return 0;
}

If you're asking why k and a are 0: i/j*j is the same as (i/j)*j . 如果你问为什么k和a为0: i/j*j(i/j)*j Since j is larger than i, i/j is 0 (integer division). 由于j大于i,因此i/j为0(整数除法)。 0*j is still 0, so the result (k) is 0. The same applies to the value of a. 0*j仍为0,因此结果(k)为0.这同样适用于a的值。

it doesn't matter if you're variable is float or not, as long you're using 如果你的变量是浮动的也没关系,只要你使用它

integer / integer , you'll get 0, 整数/整数,你会得到0,

but because you're using a float output, you get 0.0 但因为你正在使用浮动输出,你得到0.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM