I am new to visual c++ and wrote the following c++ code. I was just doing some floating point multiplication nothing more :). But I have a problem.
#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;
}
It is giving the following output.
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
Question: I am using a float array in the function'MultiplyPIArrayStored();
'. If you look at the output the value stored in array is changed (Please check the last part of the output.). Is there any problem in the array declaration? Why the array value is changing?
The array is changing because that is the nature of floating point math. If you require exact values, don't use floating point math. When floating point numbers are loaded and stored, they may be subject to precision extension or precision reduction. This can cause the ending digits to change.
If you ask two people to write "1/3" as a decimal number, they may write different things. If you ask someone to write "1/3" in six digits and double it and then ask someone else to write 2/3" in six digits, you may get ".333333" doubling to ".666666" but the person writing 2/3 might write ".6666667". If you subtract 1/3 twice from 2/3. you may get .000001 left over. That's the nature of approximate representations. Don't use them if this is not the behavior you want/need.