The assignment is to write a computer program that will add 1/3 to itself a large number of times and to compare the result to multiplying 1/3 by the number of times 1/3 was added to itself. It is also to do the same thing with ½.The program is to do this arithmetic twice, once using single precision (float) and once using double precision (double). Both of these will be in one program. Make certain you use a type for your counter that works with these large numbers.
Your program will do these additions 109 (1 billion) times.
#include<iostream>
#include<conio.h>
#include<math.h>
#include <limits>
using namespace std;
typedef std::numeric_limits< double > dbl;
int main()
{
long size=1000000000;
int count=0;
long N=10;
float nAdd=1;
float nMul=1;
cout.precision(dbl::digits10);
cout<<"Iterration #\t\tAdd\t\t\tMul"<<endl;
for(long i=1; i<=size; i++)
{
nAdd+=1.0/3.0;
nMul*=1.0/3.0;
count++;
if(count%N==0 && count!=0)
{
N*=10;
cout<<i<<"\t\t"<<fixed <<nAdd<<"\t\t"<<fixed <<nMul<<endl;
}
if(count==size)
{
cout<<"Difference : "<<fixed <<nAdd<<" - "<<fixed <<nMul<<" = "<<fixed <<nAdd-nMul<<endl;
}
}
getch();
return 0;
}
so for i have done this
i don’t get it properly
what number i have to use which will be multiply by 1/3 or 1/3 will be added into it
can you guyz explain me this a lil
thanks alot
This means that you should add ⅓ a bunch of times: ⅓ + ⅓ + ⅓ + ⅓ + …
Then you’re supposed to compare the result of that calculation with the result of multiplying ⅓ by the number of times you added it. So for example if you add it together four times (⅓ + ⅓ + ⅓ + ⅓) then you compare that result with the result of ⅓ × 4.
Mathematically the results should be the same, but the purpose of this assignment is to teach you something about how the computer performs the calculation.