0

Im in between C++ classes, so to keep practice I wrote a program to find roots.

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    double input;
    bool done = false;
    static double check;

    while (done == false)
    {
        cout << "what number to root? ";
        cin >> input;
        bool foundRoot = false;
        check = input;
        while (foundRoot == false)
        {
            if (check * check == input || check*check < input)
            {
                foundRoot = true;
                //done = true;
            }
            else
            {
                check = check - .00001;
            }
        }
        cout << "The root of " << input << " is " << setprecision(7) << check << endl;
    }

}

This program seems to return the correct values for any square tested (even non-perfects); however entering "25" returns 4.9999~. Why?

Nick
  • 29
  • 5
  • There exists `<=` – stark May 23 '20 at 11:07
  • true, I originally had the if statement spit into 2 sections for no reason in particular and combined them without thought, good catch. doesnt answer my question though – Nick May 23 '20 at 11:10
  • Have you tried running your program line by line in a debugger while monitoring the values of all variables, to see at what point the actual values deviate from the expected values? – Andreas Wenzel May 23 '20 at 11:10
  • doing so immediately equates 25 to 24.999999 and then the math just deals in repeating 9s, which leads to my question, why? – Nick May 23 '20 at 11:12
  • for example, after one iteration (subtacting 'check' by 0.1), check becomes 24.89999~ instead of 24.9. – Nick May 23 '20 at 11:17
  • @Nick: With floating point numbers, it is normal to lose a small amount of precision with every arithmetic operation. – Andreas Wenzel May 23 '20 at 11:23
  • Theoretially, it will take 2,000,000 iterations of the loop for `check` to reach the value `5`. However, with me, after 2,000,000 iterations, `check` has the value `5.0000000007571543`. This value is not exactly `5` because floating point numbers have limited precision. Because of `check` still being larger than 5, your loop will iterate one time more than mathematically appropriate, so that you will also execute the line `check = check - .00001` one time too many. Therefore, your result is about `.00001` less than what you would expect. – Andreas Wenzel May 23 '20 at 11:48

1 Answers1

2
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    double input;
    bool done = false;
    static double check;
    cout << "what number to root? ";
    cin >> input;   
    bool foundRoot = false;
    check = input;
    while (!foundRoot)
    {
        if (check * check <= input)
        {
            foundRoot = true;
        }
        else
        {
            check = check - .000001;
        }
    }
    cout << "The root of " << input << " is " << setprecision(7) << check << endl;
}

Above code gives right output. I just Increased precision. You are using brute force approach to find root. How about you use some sophisticated approach like Newton's Method.

Deep Raval
  • 377
  • 4
  • 7
  • Thank you, this works. I see now that the precision and the number I'm subtracting should be compatible. To answer your question, I've never had an exercise to create a root program, so I wanted to figure out something simple that works and then find some better way to do it – Nick May 23 '20 at 11:28