loop until two doubles differ by 0.00001

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • yogi_bear_79

    loop until two doubles differ by 0.00001

    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.
  • hurcan solter

    #2
    Re: loop until two doubles differ by 0.00001

    On Mar 11, 3:40 am, yogi_bear_79 <yogi_bear...@y ahoo.comwrote:
    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

    Comment

    • yogi_bear_79

      #3
      Re: loop until two doubles differ by 0.00001

      On Mar 10, 10:16 pm, hurcan solter <hsol...@gmail. comwrote:
      On Mar 11, 3:40 am, yogi_bear_79 <yogi_bear...@y ahoo.comwrote:
      >
      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.

      Comment

      Working...