   sqrt()
  I wanted to test a little something in C++ (a by-hand sqrt function) and it's good and all, but I see:> 100Square 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!   
  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   ;Quote:Original post by Erik Rufeltprobably 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 terminateOh, 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 need to print out more decimal places.  You're printing to 4 decimal places but finding a solution accurate to 5.;Quote:Original post by agi_shiQuote:Original post by Erik Rufeltprobably 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 terminateOh, 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++ StreamsOutputting format;Quote:Original post by Anonymous PosterYou 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.   