While loop question

I'm trying to make this code take a number(14) and a guess(3) and keep solving the problem until the answer is equal to the last.

For example,

14/3=4.666
(3+4.66)/2=3.88
14/3.88=3.652... I can get this to code, but it isn't looping for me like I want it to. I want it to just automatically keep doing it until the guess and the answer are equal.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Example program
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
double babylonian(double num, double guess);

int main(){

    cout<<babylonian(14,3);
    return 0;
}
    double babylonian(double num, double guess){
    double newGuess;
    double ans;
   while(ans!=newGuess){
       ans=num/guess;
       newGuess=(guess+ans)/2;
       ans=(num/newGuess);
       return ans;
   }
}
}
Not sure I'm going to be much help, but I do want to try to understand this myself. Here is the biggest problem I can see at the moment:

Your loop will produce the same answer no matter what you do. When it doesn't get the answer it wants, it loops through and does "14/3" because you have said ans=num/guess; which never changes, it's always 14/3. So it will then again assume newGuess is always equal to 3.83335. Which then returns the value answer again num/newGuess. 14/83335 = 3.65217 <- You're endless loop.

Edit: In order to see the endless loop, just change "return ans;" to "cout << ans << endl;"
Last edited on
The first time you reach line 16, neither ans nor newGuess has any defined value. This is part of what Edwards mentioned; initialize with num and guess before the loop and only update ans/newGuess within the loop.

However, you do have an another problem. Floating point math. That is a strange and weird beast.
You can try a==b with doubles, but it will probably fail.
Even 0.0==c is fragile. abs(c) < epsilon might work with suitable epsilon.
I hope you now have enough keywords to focus websearch on topic.
The "loop" is not a "loop" at all since line 20 will always return.

Equivalent code:

1
2
3
4
5
6
7
8
9
10
double babylonian(double num, double guess) {
    double newGuess;
    double ans;
    if ( ans != newGuess ) {
        ans = num / guess ;
        newGuess = (guess + ans) / 2;
        ans = num / newGuess ;
        return ans ;
    }
}


where you might note that both ans and newGuess have indeterminate values when they're tested on line 4 (leading to undefined behavior.) You might also note that if the condition on line 4 evaluates to false, the function returns no value (also leading to undefined behavior.)

Thanks for the help. I got it working now, if there was a better way of doing it let me know. I only changed ans/guess inside the loop and I also put the ans outside of the loop. I added a testNum value to fix the floating point problem kesk spoke of. However it seemed to work both with and without the testNum value. The second code is below.


(newGuess!=guess) and removed the differenceGuess line.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// Example program
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
double babylonian(double num, double guess);

int main(){

    cout<<babylonian(14,3);
    return 0;
}
    double babylonian(double num, double guess){
    double ans;
    double testNum=.001;
    double differenceGuess=1;
    double oldGuess;

   while(differenceGuess>testNum){
       ans=num/guess;
       oldGuess=guess;
       guess=(oldGuess+ans)/2;
       differenceGuess=fabs(oldGuess-guess)/guess;

   }
                 return ans;

}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
}
    double babylonian(double num, double guess){
    double ans;
    double oldGuess;

   while(guess!=oldGuess){
       ans=num/guess;
       oldGuess=guess;
       guess=(oldGuess+ans)/2;

   }
                 return ans;

}
Last edited on
Topic archived. No new replies allowed.