0

Tried to code bisection method to compute square root of 2, till the difference between two consecutive steps is less than some specific value, say 10^{-11}, where tolerance variable is specified as 11.

But, inspite of choosing long double data type,am unable to get beyond 6 places after decimal.

The same result is obtained, if choose tolerance as 11, 30, 5.

Cannot understand the reason for that.

The reason for that is unclear, as know that can get infinite number of decimal digits in the decimal expansion of sqrt(2).

Am running on onlinegdb.com, using the C++ option.

Each of the three values of tolerance gave the result as: 1.41421.

#include <iostream>
#include <math.h>
using namespace std;

int main() {
   int val, tolerance;
   cout << "Input the values of the number whose square root need be found, and the tolerance limit in the number of digits, of integer variable: tolerance, giving tolerance limit as: 10^{-tolerance}" << endl;
   cout<< "Enter the number, followed by the # tolerance digits."<< endl;
   cin >>val>> tolerance;
   if(val< 0)
      exit(0); 
   else 
   {
      long double low = 0, high = val;
      long double  mid = (low + high)/2;
      int c = 0; 
      long double diff = 0;

      while (!c) 
      {
         if((mid*mid-val)<0)
            diff = val - mid*mid;
         if (diff <= pow(10, -1*tolerance))
         {
            c = 1;
            cout << "Square root of 2= "<< mid<<endl;
         }
         else if(mid * mid > val) 
         {
            high = mid;
            mid = (low + high)/2;      
         } 
         else 
         {
            low = mid;
            mid = (low + high)/2;
         }
      }
    }
 return 0;
} 
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
jitender
  • 109
  • 2
  • @DrewDormann Yes, but not works here seems. Have removed these, hence. That means : square root(2), or in C: sqrt(2). – jitender Mar 19 '23 at 02:17
  • Not sure if it's _the_ issue, but there seems to be a hole in your logic: if `((mid*mid-val)<0)` is false you use the `diff` value of the previous iteration. – 500 - Internal Server Error Mar 19 '23 at 02:21
  • Is the issue here really the value itself, or what was printed to cout? After all the default printing precision is quite low – harold Mar 19 '23 at 02:24
  • @harold The printed value by cout is the same, irrespective of tolerance. Unable to increase the number of digits after the decimal point. – jitender Mar 19 '23 at 02:26
  • Have you tried just [printing more decimals](https://stackoverflow.com/q/554063/555045) – harold Mar 19 '23 at 02:26
  • @harold yes, it worked by adding in header: #include , and: typedef std::numeric_limits< double > dbl; And in the code: cout.precision(dbl::max_digits10); Should I remove the question then? – jitender Mar 19 '23 at 02:47
  • cout displays for tolerance=13 > 1.414213562373078. And for tolerance= 11, prints> 1.4142135623696959. – jitender Mar 19 '23 at 02:54
  • @harold Is there is a way to print 100 decimal digits? – jitender Mar 19 '23 at 03:05
  • 1
    Yes, but will just show you a more exact conversion of the `long double`, it won't give you a square root to a precision of 100 digits since that cannot be stored in a `long double` to begin with. You can do it, but if you want to print lots of digits of a square root, that wouldn't be the way. – harold Mar 19 '23 at 03:39
  • @harold Am waiting for your reply to my last comment, but want to add that if taken output in a file, then no such additional precision (as obtained for cout) is obtained. So, is there a way possible for that too? – jitender Mar 19 '23 at 04:19

0 Answers0