Visual C ++浮点数组值的变化

时间:2012-07-21 07:12:14

标签: c++ visual-c++ visual-c++-2010

我是visual c ++的新手,并编写了以下c ++代码。我只是做了一些浮点乘法而已。但我有一个问题。

#include "stdafx.h"
#define PI 3.14F
#define totalRound 10.00F

void MultiplyPIArrayStored()
{
    printf("\n\nAnalysis\n");

    float* defArr = new float[(int)totalRound];
    float inc=0.00F;
    for(float i=1.00F;i<=totalRound;i++)
    {
        defArr[(int)i]=i*PI;
        //printf("Calculation: #define => %f * %f = %f\n",i,PI,i*PI);
    }

    float lPI=3.14F;
    for(float i=1.00F;i<=totalRound;i++)
    {
        //printf("Calculation: local variable => %f * %f = %f\n",i,lPI,i*lPI);
        printf("#define =>%f; local variable=>%f\n",defArr[(int)i],i*lPI);
        if(defArr[(int)i]==i*lPI)
            inc++;
    }

    printf("\nequal rate %f percentage",(inc/totalRound)*100);
    printf("\ndifference rate %f percentage",((totalRound-inc)/totalRound)*100);
}

void MultiplyPI()
{
    printf("\n\nAnalysis\n\n");

    float lPI=3.14F;
    float inc=0.00F;

    for(float i=1.00F;i<=totalRound;i++)
    {
        printf("\nCalculation: #define => %f * %f = %f\n",i,PI,i*PI);
        printf("Calculation: local variable => %f * %f = %f\n",i,lPI,i*lPI);
        printf("#define => %f ; local variable => %f\n",i*PI,i*lPI);
        if(i*PI==i*lPI)
            inc++;
    }

    printf("\nEqual rate %f percentage",(inc/totalRound)*100);
    printf("\nDifference rate %f percentage",((totalRound-inc)/totalRound)*100);
}

int _tmain(int argc, _TCHAR* argv[])
{
    MultiplyPI();
    getchar();

    MultiplyPIArrayStored();
    getchar();

    return 0;
}

它提供以下输出。

Analysis


Calculation: #define => 1.000000 * 3.140000 = 3.140000
Calculation: local variable => 1.000000 * 3.140000 = 3.140000
#define => 3.140000 ; local variable => 3.140000

Calculation: #define => 2.000000 * 3.140000 = 6.280000
Calculation: local variable => 2.000000 * 3.140000 = 6.280000
#define => 6.280000 ; local variable => 6.280000

Calculation: #define => 3.000000 * 3.140000 = 9.420000
Calculation: local variable => 3.000000 * 3.140000 = 9.420000
#define => 9.420000 ; local variable => 9.420000

Calculation: #define => 4.000000 * 3.140000 = 12.560000
Calculation: local variable => 4.000000 * 3.140000 = 12.560000
#define => 12.560000 ; local variable => 12.560000

Calculation: #define => 5.000000 * 3.140000 = 15.700001
Calculation: local variable => 5.000000 * 3.140000 = 15.700001
#define => 15.700001 ; local variable => 15.700001

Calculation: #define => 6.000000 * 3.140000 = 18.840001
Calculation: local variable => 6.000000 * 3.140000 = 18.840001
#define => 18.840001 ; local variable => 18.840001

Calculation: #define => 7.000000 * 3.140000 = 21.980001
Calculation: local variable => 7.000000 * 3.140000 = 21.980001
#define => 21.980001 ; local variable => 21.980001

Calculation: #define => 8.000000 * 3.140000 = 25.120001
Calculation: local variable => 8.000000 * 3.140000 = 25.120001
#define => 25.120001 ; local variable => 25.120001

Calculation: #define => 9.000000 * 3.140000 = 28.260001
Calculation: local variable => 9.000000 * 3.140000 = 28.260001
#define => 28.260001 ; local variable => 28.260001

Calculation: #define => 10.000000 * 3.140000 = 31.400001
Calculation: local variable => 10.000000 * 3.140000 = 31.400001
#define => 31.400001 ; local variable => 31.400001

Equal rate 100.000000 percentage
Difference rate 0.000000 percentage


Analysis
#define =>3.140000; local variable=>3.140000
#define =>6.280000; local variable=>6.280000
#define =>9.420000; local variable=>9.420000
#define =>12.560000; local variable=>12.560000
#define =>15.700001; local variable=>15.700001
#define =>18.840000; local variable=>18.840001
#define =>21.980001; local variable=>21.980001
#define =>25.120001; local variable=>25.120001
#define =>28.260000; local variable=>28.260001
#define =>31.400002; local variable=>31.400001

equal rate 40.000000 percentage
difference rate 60.000000 percentage

问题:我在函数“MultiplyPIArrayStored();”中使用了一个float数组。如果查看输出,则更改存储在数组中的值(请检查输出的最后部分。)。数组声明中有问题吗?为什么数组值正在改变?

1 个答案:

答案 0 :(得分:1)

数组正在发生变化,因为这是浮点数学的本质。如果需要精确值,请不要使用浮点数学。当加载和存储浮点数时,它们可能会受到精确扩展或精度降低的影响。这可能导致结束数字发生变化。

如果你要求两个人写“1/3”作为十进制数字,他们可能会写不同的东西。如果你要求别人用六位数字写“1/3”并加倍,然后让别人写下6/3“2/3”,你可能会得到“.333333”加倍“.666666”但写作2的人/ 3可能写“.6666667”。如果你从2/3中减去1/3两次。你可能会得到.000001。这就是近似表示的本质。如果这不是你想要的行为,请不要使用它们/需要。