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

Dani AI

Generated

Short summary and key corrections (ties to existing replies)
The task is: start at 0, add 1/3 to the accumulator N times, and compare that result to (1/3) N — once in single precision and once in double. In ’s first post the accumulator is wrongly initialized to 1 (should be 0) and the “multiplication” loop uses repeated `=which computes (1/3)^N, not (1/3)*N. ’s suggestion (do the loop for the sum and compute the product once) is the right approach. is correct that10*9` is 90; the intended value is 10^9 (1,000,000,000). is also right in principle: in exact arithmetic the difference is zero — any nonzero difference is a floating-point rounding effect.

Why the float stops growing at 8,388,608
Single-precision (IEEE‑754) has a 24‑bit significand (23 stored bits + implicit 1). The spacing (ULP) at magnitude x is roughly U = 2^(floor(log2(x)) − 23). When x >= 2^23 = 8,388,608 the spacing becomes 1, so adding 1/3 (≈0.333) is smaller than half the spacing and typically gets rounded away. That explains the plateau seen in the screenshot: repeated addition ceases to change the float, while a single multiply follows a different rounding path and can produce a different final float.

Practical checks and a fix (examples)
To detect where addition no longer changes the accumulator, use a loop that checks if (sum == before) break;. To improve accuracy across many tiny increments, use compensated summation (Kahan) or do a pairwise/blocked sum. Example fragments:

uint64_t N = 1000000000ULL;
float v = 1.0f/3.0f;
float s = 0.0f;
for (uint64_t i = 0; i < N; ++i) {
    float before = s;
    s += v;
    if (s == before) { std::cout << "stops at i=" << i << " value=" << s << '\n'; break; }
}
double kahan_sum(uint64_t N, double v) {
    double sum = 0.0, c = 0.0;
    for (uint64_t i = 0; i < N; ++i) {
        double y = v - c;
        double t = sum + y;
        c = (t - sum) - y;
        sum = t;
    }
    return sum;
}

Practical tips
Use a 64‑bit loop counter (uint64_t) for N=1e9, initialize accumulators to 0, compute the exact-comparison value as expected = double(N) * (1.0/3.0), and print with std::setprecision(std::numeric_limits<T>::max_digits10). Test with much smaller N first — 1e9 iterations are slow. For numerical correctness prefer double or compensated algorithms when summing many small values.

Recommended Answers

All 5 Replies

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 :)
10*9 is 90 not 1 billion :) 10^9 may b :)
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.