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

Something like this:

enum { N = 1024*1024*1024 } ; // 1G times
double value = 1.0 / 3.0 ;

double plus_result = 0 ;
for( int i=0 ; i < N ; ++i ) plus_result += value ;

const double multiplies_result = value * N ;

std::cout << std::fixed << std::setprecision( std::numeric_limits< double >::digits10 )
          << "plus_result: " << plus_result << '\n'
          << "multiplies_result: " << multiplies_result << '\n' ;

"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" & "Your program will do these additions 109 (1 billion) times." (perhaps they meant to say 10 x 9?)

so what I understand is this:

double Start = (1/3);
double Addition = (1/3);

double Multiplication = Start * (10 * 9);

for (long i = 0; i < (10 * 9); i++){

    Start = Start + Addition;
}

//compare Start to Multiplication

thanks guyz :)
@Eagletalon 10*9 is 90 not 1 billion :) 10^9 may b :)
@vijayan121 thanks alot

when i use this code with float i get much larger difference ... why is it so?
coz of float range?

using double error is around -0.33

float

how float Start variable stop growing at 8388608

but float Multiplication i.e (1.0/3.0)*1000000000 can hold this larger value

I understand the task as to see the final value (which would be 0 with infinite precission) with two precission of one_third:

add_10_billion_times(one_third) - 10E9 / 3.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.