C语言 为什么将两个整数相除不会得到浮点数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16221776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why dividing two integers doesn't get a float?
提问by mushroom
Can anyone explain why b gets rounded off here when I divide it by an integer although it's a float?
谁能解释为什么当我将它除以整数时 b 在这里四舍五入,尽管它是一个浮点数?
#include <stdio.h>
void main() {
int a;
float b, c, d;
a = 750;
b = a / 350;
c = 750;
d = c / 350;
printf("%.2f %.2f", b, d);
// output: 2.00 2.14
}
回答by Sukrit Kalra
This is because of implicit conversion. The variables b, c, dare of floattype. But the /operator sees two integers it has to divide and hence returns an integer in the result which gets implicitly converted to a floatby the addition of a decimal point. If you want float divisions, try making the two operands to the /floats. Like follows.
这是因为隐式转换。变量b, c, d是float类型的。但是/运算符看到它必须除以两个整数,因此在结果中返回一个整数,该整数float通过添加小数点隐式转换为 a 。如果您想要浮点数除法,请尝试将两个操作数设置为/浮点数。如下。
#include <stdio.h>
int main() {
int a;
float b, c, d;
a = 750;
b = a / 350.0f;
c = 750;
d = c / 350;
printf("%.2f %.2f", b, d);
// output: 2.14 2.14
return 0;
}
回答by Cacho Santa
Use castingof types:
使用铸造的类型:
int main() {
int a;
float b, c, d;
a = 750;
b = a / (float)350;
c = 750;
d = c / (float)350;
printf("%.2f %.2f", b, d);
// output: 2.14 2.14
}
This is another way to solve that:
这是解决这个问题的另一种方法:
int main() {
int a;
float b, c, d;
a = 750;
b = a / 350.0; //if you use 'a / 350' here,
//then it is a division of integers,
//so the result will be an integer
c = 750;
d = c / 350;
printf("%.2f %.2f", b, d);
// output: 2.14 2.14
}
However, in both cases you are telling the compiler that 350 is a float, and not an integer. Consequently, the result of the division will be a float, and not an integer.
但是,在这两种情况下,您都在告诉编译器 350 是浮点数,而不是整数。因此,除法的结果将是一个浮点数,而不是一个整数。
回答by Daniel Santos
Specifically, this is not rounding your result, it's truncating toward zero. So if you divide -3/2, you'll get -1 and not -2. Welcome to integral math! Back before CPUs could do floating point operations or the advent of math co-processors, we did everything with integral math. Even though there were libraries for floating point math, they were too expensive (in CPU instructions) for general purpose, so we used a 16 bit value for the whole portion of a number and another 16 value for the fraction.
具体来说,这不是四舍五入您的结果,而是向零截断。所以如果你除以 -3/2,你会得到 -1 而不是 -2。欢迎来到积分数学!在 CPU 可以进行浮点运算或数学协处理器出现之前,我们用积分数学完成了所有工作。尽管有用于浮点数学的库,但它们对于一般用途来说太昂贵了(在 CPU 指令中),所以我们使用 16 位值表示数字的整个部分,另外使用 16 位值表示分数。
EDIT: my answer makes me think of the classic old man saying "when I was your age..."
编辑:我的回答让我想起了经典的老人说“当我像你这么大的时候……”
回答by John Bode
6.5.5 Multiplicative operators
...
6 When integers are divided, the result of the/operator is the algebraic quotient with any fractional part discarded.105)If the quotienta/bis representable, the expression(a/b)*b + a%bshall equala; otherwise, the behavior of botha/banda%bis unde?ned.
105) This is often called ‘‘truncation toward zero''.
Dividing an integer by an integer gives an integer result. 1/2 yields 0; assigning this result to a floating-point variable gives 0.0. To get a floating-point result, at least one of the operands must be a floating-point type. b = a / 350.0f;should give you the result you want.
一个整数除以一个整数给出一个整数结果。1/2 产生 0;将此结果分配给浮点变量会得到 0.0。要获得浮点结果,至少一个操作数必须是浮点类型。 b = a / 350.0f;应该给你你想要的结果。
回答by Goran Belfinger
"a" is an integer, when divided with integer it gives you an integer. Then it is assigned to "b" as an integer and becomes a float.
“a”是一个整数,当除以整数时,它会给你一个整数。然后将其作为整数分配给“b”并成为浮点数。
You should do it like this
你应该这样做
b = a / 350.0;
回答by kyle_13
Dividing two integers will result in an integer (whole number) result.
将两个整数相除将产生一个整数(整数)结果。
You need to cast one number as a float, or add a decimal to one of the numbers, like a/350.0.
您需要将一个数字转换为浮点数,或者在其中一个数字上添加一个小数,例如 a/350.0。
回答by R.. GitHub STOP HELPING ICE
Probably the best reason is because 0xfffffffffffffff/15would give you a horribly wrong answer...
可能最好的原因是因为0xfffffffffffffff/15会给你一个可怕的错误答案......

