• Advertisement
Sign in to follow this  

sqrt()

This topic is 4287 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I wanted to test a little something in C++ (a by-hand sqrt function) and it's good and all, but I see:

> 100


Square root of 100:
{
        a_1     =       50
        a_2     =       26
        a_3     =       14.9231
        a_4     =       10.8121
        a_5     =       10.0305
        a_6     =       10
        a_7     =       10
}


>
Why is it that I get a a_6 = 10 and then a a_7 = 10 as well? Code:
#include <iostream>
using namespace std;

inline long double abs(long double n) {
    return (n < (long double)0.0) ? n * (long double)-1.0 : n;
}

bool ok(long double a, long double n) {
    if (abs(abs(n) - abs(a * a)) < (long double)0.00001)
        return true;
    return false;
}

long double sqrt(long double n) {
    long double a = n * (long double)0.5;

    cout << "\n\nSquare root of " << n << ":\n";
    cout << "{\n";

    for (unsigned i = 1; /*!ok(a, n)*/; i++) {
        cout << '\t' << "a_" << i << "\t=\t" << a << "\n";

        if (!ok(a, n))
            a = (a + (n / a)) * (long double)0.5;
        else
            break;
    }

    cout << "}\n\n";

    return a;
}

int main() {
	while (true) {
	    cout << "\n> ";

	    long double n;
	    cin >> n;
	    cin.ignore();

	    sqrt(n);
	}
}

Any ideas? If it's "ok" it should end it, not repeat it... (Same for 900 [30], 10000 [100], etc.) Thanks!

Share this post


Link to post
Share on other sites
Advertisement
probably because u have 5 decimal digits in your < 0.00001 but u only print 4, so it will round to 10 before it's actually close enough to terminate

Share this post


Link to post
Share on other sites
Quote:
Original post by Erik Rufelt
probably because u have 5 decimal digits in your < 0.00001 but u only print 4, so it will round to 10 before it's actually close enough to terminate


Oh, so the double still calculates great, but I can't acutally see it? If so, how can cout output all of a long double?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You need to print out more decimal places. You're printing to 4 decimal places but finding a solution accurate to 5.

Share this post


Link to post
Share on other sites
Quote:
Original post by agi_shi
Quote:
Original post by Erik Rufelt
probably because u have 5 decimal digits in your < 0.00001 but u only print 4, so it will round to 10 before it's actually close enough to terminate


Oh, so the double still calculates great, but I can't acutally see it? If so, how can cout output all of a long double?


You can use cout's precision() member function, or std::setprecision (you'll need to #include <iomanip>)

Formatting Your Output with C++ Streams
Outputting format

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
You need to print out more decimal places. You're printing to 4 decimal places but finding a solution accurate to 5.


How would I go about outputting more decimal places? I searched some, and could only find setf(), but nothing else in particular... Any ideas?

EDIT: Too slow; thanks Will F.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement