为什么分割两个整数不能得到浮点数?(复制)

时间:2021-07-11 20:58:20

This question already has an answer here:

这个问题已经有了答案:

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
}

http://codepad.org/j1pckw0y

http://codepad.org/j1pckw0y

7 个解决方案

#1


27  

This is because of implicit conversion. The variables b, c, d are of float type. But the / operator sees two integers it has to divide and hence returns an integer in the result which gets implicitly converted to a float by 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。就像之前。

#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;
}

#2


9  

Use casting of 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是一个浮点数,而不是整数。因此,除法的结果将是一个浮点数,而不是一个整数。

#3


2  

"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;

#4


2  

Chapter and verse

引经据典

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 quotient a/b is representable, the expression (a/b)*b + a%b shall equal a; otherwise, the behavior of both a/b and a%b is undefined.

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;应该给你你想要的结果。

#5


1  

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..."

编辑:我的回答让我想起了那个经典的老人说的“当我像你这么大的时候……”

#6


0  

Probably the best reason is because 0xfffffffffffffff/15 would give you a horribly wrong answer...

可能最好的原因是0xfffffffffff /15会给你一个非常错误的答案……

#7


0  

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。

#1


27  

This is because of implicit conversion. The variables b, c, d are of float type. But the / operator sees two integers it has to divide and hence returns an integer in the result which gets implicitly converted to a float by 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。就像之前。

#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;
}

#2


9  

Use casting of 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是一个浮点数,而不是整数。因此,除法的结果将是一个浮点数,而不是一个整数。

#3


2  

"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;

#4


2  

Chapter and verse

引经据典

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 quotient a/b is representable, the expression (a/b)*b + a%b shall equal a; otherwise, the behavior of both a/b and a%b is undefined.

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;应该给你你想要的结果。

#5


1  

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..."

编辑:我的回答让我想起了那个经典的老人说的“当我像你这么大的时候……”

#6


0  

Probably the best reason is because 0xfffffffffffffff/15 would give you a horribly wrong answer...

可能最好的原因是0xfffffffffff /15会给你一个非常错误的答案……

#7


0  

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。