Click here to Skip to main content
15,896,915 members

Comments by VaKa (Top 5 by date)

VaKa 23-Mar-24 15:29pm View    
im writing here not for stars, but to point out that for-range size not enough to rich right result.
for-range size is primary problem, despite other mistakes.
why i say that? because without understanding that, all other mistakes had no chance to be understood and corrected.

sorry for pure english.
peace to all of you!
VaKa 22-Mar-24 15:23pm View    
i'm just trying to say, that problem not in variable storing format (double or float), but in realization as well.

just compile and run:
    double formula, s1, v1, s2;
    double k;
    double kInc;

    // supposing initial values
    kInc = 0.00001;
    k    =  0.0;
    s1   = -5.0;
    v1   =  2.0;
    s2   =  1.0;

    const double epsilon = 1.0E-6;

    for(int i = -10000; i < 400000; i++) 
    {
        formula = s1 / (v1 + k) + s2;

        if( fabs(formula) < epsilon)
        {
            break;
        }

         k += kInc;
    }

    printf("\n\nResult: %f (i=%d, cycles: %d) => formula=%f\n\n", k, i, int(k/kInc), formula);


    // output
    // >>
    //     Result: 3.000000 (i=290000, cycles: 300000) => formula=0.000000
VaKa 22-Mar-24 6:36am View    
As i get it right, i should post my comment as a separate solution. So let it be
VaKa 21-Mar-24 6:13am View    
i think problem described here is more in math than programming, but Every Computer Scientist Should Know About Floating-Point Arithmetic ^)
VaKa 21-Mar-24 3:07am View    
comment moved to separate solution (Solution 6)