while loop not working?

I am trying to do Newton's method. What is wrong with my code? The while loop is not executing correctly. The value of the currNextRatio that terminates the while loop is still well above where it is required to be when the program finishes. I have left in my tests.

source code:
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <math.h>
#include <stdlib.h>

using namespace std;

int main()
{
    int degree;
    float guess;
    int n, n2;

    cout << "Enter the degree of the highest degree term:  ";
    cin >> degree;

    float coef[degree];

    for(n = degree; n >= 0; n--)
    {
        cout << "Enter the coefficient of the next highest degree term:  ";
        cin >> coef[n];

        cout << coef[n] << "x^" << n << endl;
    }

    for(n2 = degree; n2 >= 0; n2--)
    {
        if (n2 == 0)
        {
            cout << " " << coef[n2] << endl;
            break;
        }
        cout << " " << coef[n2] << "x^" << n2 << " +";
    }

    cout << "Guess a root.  The computer will find the root closest to this number.  ";
    cin >> guess;


    float nextValue = guess;
    float currValue;
    float currNextRatio = 2;

    while (abs(currNextRatio - 1.0) > 0.00001)
    {
        currValue = nextValue;

        float polyValue = 0;
        int n3;
        for (n3 = degree; n3 >= 0; n3--)
        {
            polyValue += coef[n3] * pow(currValue, n3);
        }
        float polynomial = polyValue;

        polyValue = 0;
        int n4;
        for (n4 = degree; n4 >= 1; n4--)
        {
            polyValue += coef[n4] * n4 * pow(currValue, n4 - 1);
        }
        float polyPrime = polyValue;

        nextValue = currValue - (polynomial / polyPrime);
        currNextRatio = (currValue - nextValue) / currValue;

        char enter;
        cout << "nextValue: " << nextValue << ", currValue: " << currValue << endl;
        cin >> enter;
    }

    cout << "The closest root to that value is " << endl << nextValue << endl;
    cout << "nextValue: " << nextValue << ", currValue: " << currValue << " ratio: " << currNextRatio << endl;

    return 0;
}


output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Enter the degree of the highest degree term:  2
Enter the coefficient of the next highest degree term:  5
5x^2
Enter the coefficient of the next highest degree term:  4
4x^1
Enter the coefficient of the next highest degree term:  3
3x^0
 5x^2 + 4x^1 + 3
Guess a root.  The computer will find the root closest to this number.  6
nextValue: 2.76562, currValue: 6
m
The closest root to that value is
2.76562
nextValue: 2.76562, currValue: 6 ratio: 0.539062

Process returned 0 (0x0)   execution time : 12.927 s
Press any key to continue.


The two pieces which do not match are the while loop
while (abs(currNextRatio - 1.0) > 0.00001)
and the ratio output which is the final value of currNextRatio
ratio: 0.539062

is abs() making an int out of my float?
Yup. Strange how asking a question can somehow help to see the problem. Though I think I still have my math wrong.

I have now replaced
#include <stdlib.h>
with
#include <cmath>
to get abs to work with a float.

I was also wrong in thinking I needed a ratio for the while loop. All I needed to do was take the abs of the difference between currValue and nextValue.
Last edited on
Topic archived. No new replies allowed.