loop until two doubles differ by 0.00001

Discussion in 'C++' started by yogi_bear_79, Mar 11, 2008.

  1. yogi_bear_79

    yogi_bear_79 Guest

    I am working on divide-and-average lab. I everything done excep the
    syntax for the loop. The loop should run replaceing x with the resutls
    of (x + a/x )/2 until x, and (x + a/x )/2 differ by no more than a
    given amout, for example 0.00001. I've tried various ideas, the
    closest I came was while(x - divavg(a, x) != 0), which more or les
    sworked. It stoppend the loop when x = (x + a/x )/2, but I'm not sure
    that exactly meets the goal.
    yogi_bear_79, Mar 11, 2008
    #1
    1. Advertising

  2. On Mar 11, 3:40 am, yogi_bear_79 <> wrote:
    > I am working on divide-and-average lab. I everything done excep the
    > syntax for the loop. The loop should run replaceing x with the resutls
    > of (x + a/x )/2 until x, and (x + a/x )/2 differ by no more than a
    > given amout, for example 0.00001. I've tried various ideas, the
    > closest I came was while(x - divavg(a, x) != 0), which more or les
    > sworked. It stoppend the loop when x = (x + a/x )/2, but I'm not sure
    > that exactly meets the goal.


    something like,

    fabs(x-divavg(a,x)) < 0.00001;

    and make it a function
    hurcan solter, Mar 11, 2008
    #2
    1. Advertising

  3. yogi_bear_79

    yogi_bear_79 Guest

    On Mar 10, 10:16 pm, hurcan solter <> wrote:
    > On Mar 11, 3:40 am, yogi_bear_79 <> wrote:
    >
    > > I am working on divide-and-average lab.  I everything done excep the
    > > syntax for the loop. The loop should run replaceing x with the resutls
    > > of (x + a/x )/2 until x,  and (x + a/x )/2 differ by no more than a
    > > given amout, for example 0.00001.  I've tried various ideas, the
    > > closest I came was while(x - divavg(a, x)  != 0), which more or les
    > > sworked. It stoppend the loop when x =  (x + a/x )/2, but I'm not sure
    > > that exactly meets the goal.

    >
    > something like,
    >
    >  fabs(x-divavg(a,x)) < 0.00001;
    >
    > and make it a function


    Thanks, while(fabs(x-divavg(a, x)) > 0.00001) works fine, except it
    stops one short, I've also tried a do/while same results, any syntax I
    am missing so I don't have to put one more cout statement outside the
    loop to catch the last iteration.
    yogi_bear_79, Mar 11, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. {AGUT2} {H}-IWIK
    Replies:
    4
    Views:
    2,830
    Marcelo Pinto
    Sep 12, 2003
  2. nicolas

    Comparing two doubles

    nicolas, Sep 20, 2003, in forum: C++
    Replies:
    3
    Views:
    4,084
    Jacek Dziedzic
    Sep 21, 2003
  3. dan
    Replies:
    1
    Views:
    2,295
    Jack Klein
    Nov 26, 2003
  4. Replies:
    2
    Views:
    106
    Anno Siegel
    Aug 1, 2005
  5. Isaac Won
    Replies:
    9
    Views:
    343
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page