为什么这个浮点数有2个不同的值? [英] Why does this float have 2 different values?

查看:58
本文介绍了为什么这个浮点数有2个不同的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我这样做时:

  float add = 0;int count [] = {3,2,1,3,1,2,3,3,1,2,1}对于(int i = 0; i< 11; i ++)加+ = 1/((float)count + 1); 

输出为:

  4.00000000000000000000 

但是当我这样做时:

  float add = 0;int count [] = {3,2,1,3,1,2,3,3,1,2,1}对于(int i = 0; i< 11; i ++)加+ = 1.0/((float)count + 1); 

输出为:

  3.99999976158142089844 

当我需要将一个int转换为浮点数时,我可以在前面添加(float)或让它使用小数进行算术运算,例如 a/1.0 .有什么区别吗?

添加所需的行为.

原因是在此之后,我需要一个将 add 添加到另一个int变量以输出int的结果.但是,当我以第二种方式进行操作时,int使用3而不是4,因此我想知道第一和第二个代码之间的区别.

解决方案

您的代码不是C,但这是:

  #include< stdio.h>int main(){浮点加= 0;int count [] = {3,2,1,3,1,2,3,3,1,2,1};对于(int i = 0; i< 11; i ++){加+ = 1/((float)count [i] +1);}printf(%f \ n",add);返回0;} 

我用 add + = 1/((float)count [i] + 1); add + = 1.0/((float)count [i]] + 1); .

在两种情况下, printf(%f \ n",add); 打印 4.000000 .

但是,当我打印变量 add 的每一位时,它会给我 010000000111111111111111111111111 (3.9999998)和 01000000100000000000000000000000 (4)

如phuclv所指出的,这是因为 1.0 double ,因此计算的精度是双精度的,而使用 1 时,计算是使用单精度完成的(因为强制转换为浮动).

如果在第一个方程式中将转换替换为 double ,或者在第二个方程式中将 1.0 更改为 1.0f ,则会得到相同的结果.

When I do this:

float add=0;

int count[] = {3, 2, 1, 3, 1, 2, 3, 3, 1, 2, 1}
for (int i=0; i<11; i++)
    add += 1 / ((float)count+1);

The output is:

4.00000000000000000000

But when I do this:

float add=0;

int count[] = {3, 2, 1, 3, 1, 2, 3, 3, 1, 2, 1}
for (int i=0; i<11; i++)
    add += 1.0 / ((float)count+1);

The output is:

3.99999976158142089844

When I need to turn an int into a float, I either add (float) in front or let it do arithmetic with a decimal such as a / 1.0. Is there any difference?

Edit: Adding the desired behaviour.

The reason is that afterwards, I need a result that adds add to another int variable for an int output. However, when I do it the second way, the int uses 3 instead of 4 so I would like to know what is the difference between the first and second code.

解决方案

Your code isn't C, but this is:

#include <stdio.h>

int main ()
{
        float add = 0;
        int count[] = { 3, 2, 1, 3, 1, 2, 3, 3, 1, 2, 1 };
        for (int i = 0; i < 11; i++) {
                add += 1 / ((float) count[i] + 1);
        }
        printf("%f\n", add);
        return 0;
}

I've executed this code with add += 1 / ((float) count[i] + 1); and add += 1.0 / ((float) count[i] + 1);.

In both cases, printf("%f\n", add); prints 4.000000.

However, when I print each bit of the variable add, it gives me 01000000011111111111111111111111 (3.9999998) and 01000000100000000000000000000000 (4)

As pointed by phuclv, this is because 1.0 is a double, hence the calculation is done with double precision, whereas when using 1, the calculation is done using single precision (because of the cast to float).

If you replace the cast to double in the first equation or if you change 1.0 into 1.0f in the second equation, you'll obtain the same result.

这篇关于为什么这个浮点数有2个不同的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆