0

I want to get square root of a number upto 9 precision points so I did something like below but I am not getting correct precision. Here e is the precision which is greater than 10^9 then also ans is upto 5 precision points. What am I doing wrong here??

    #include <iostream>
    using namespace std;
    long double squareRoot(long double n) 
        { 

            long double x = n; 
            long double y = 1; 
            long double e = 0.00000000000001; 
            while (x - y > e) 
            { 
                x = (x + y) / 2; 
                y = n / x; 
            } 
            cout << x << "\n";
            return x; 
        } 

    int main() 
    {
        int arr[] = {2,3,4,5,6};
        int size = sizeof(arr)/sizeof(arr[0]);
        long double ans = 0.0;
        for(int i=0; i<size; i++)
        {
            ans += squareRoot(arr[i]);
        }

        cout << ans << "\n";
        return 0;
    }

The output is

1.41421
1.73205
2
2.23607
2.44949
9.83182

What should I do to get precision upto 9 points??

darkexodus
  • 147
  • 1
  • 14
  • does not seem the precision is too small, you just don't format it properly. https://stackoverflow.com/questions/33125779/format-double-value-in-c – bartop Nov 06 '19 at 08:25
  • 2
    You can set the precision _of the output_ by calling `std::cout << std::setprecision(9)` if that's what you mean. – Albjenow Nov 06 '19 at 08:26
  • 2
    Use [I/O manipulators](https://en.cppreference.com/w/cpp/io/manip) to [set the precision](https://en.cppreference.com/w/cpp/io/manip/setprecision) of the output? – Some programmer dude Nov 06 '19 at 08:26
  • @Albjenow but I want to add the square root of elements in an array. Using I/O manip won't work here. – darkexodus Nov 06 '19 at 08:28
  • I want to add the square root of elements in the given array. The summation of that should be precise upto 9 points. How does I/O manip is useful here?? – darkexodus Nov 06 '19 at 08:31
  • The problem is that with your current code additional digits are simply not shown and instead rounded off. According to [Wolfram Alpha](https://www.wolframalpha.com/input/?i=sqrt%282%29+%2B+sqrt%283%29+%2B+sqrt%284%29+%2B+sqrt%285%29+%2B+sqrt%286%29) your result is indeed correct upto 15 digits. – Albjenow Nov 06 '19 at 08:35
  • @Albjenow In that case what should I do now? – darkexodus Nov 06 '19 at 08:38
  • Off-topic: about [using namespace std](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)... – Aconcagua Nov 06 '19 at 09:12

2 Answers2

1

There are two places at which precision plays a role:

  • precision of the value itself
  • precision of the output stream

You can only get output in desired precision if both value and stream are precise enough.

In your case, the calculated value doesn't seem to be a problem, however, default stream precision is only five digits, i. e. no matter how precise your double value actually is, the stream will stop after five digits, rounding the last one appropriately. So you'll need to increase stream precision up to the desired nine digits:

std::cout << std::setprecision(9);
// or alternatively:
std::cout.precision(9);

Precision is kept until a new one is set, in contrast to e. g. std::setw, which only applies for next value.

Aconcagua
  • 24,880
  • 4
  • 34
  • 59
0

try this

cout << setprecision(10) << x << "\n";

cout << setprecision(10) << ans << "\n";
Ahmed Anter
  • 650
  • 4
  • 13