extra long numbers ?

Hello,
I have a question about the length of the output.
The code works but...

1
2
3
4
5
6
7
8
9
  #include <iostream>
using namespace std;
template<class T>
T Add(T a,T b){return a+b;}
int main()
{
   cout<<Add<long double>(-5.553892398912181215,8.238822124218157451258)<<"\n";
   cout << 9.256812548962351255252251;
}

Why is the output limited to 5 digits?

1
2
     output: 2.68493
             9.25681
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <iomanip>

template<class T>
T Add(T a,T b){return a+b;}

int main()
{
   std::cout << std::fixed /* fixed point notation */ << std::setprecision(20) // with 20 digits after the decimal point
             << Add<long double>(-5.553892398912181215,8.238822124218157451258) << '\n' ;
}

http://coliru.stacked-crooked.com/a/74c40b5bbbf4acd4

See: https://stdcxx.apache.org/doc/stdlibug/28-3.html
thank u :) great tutorial btw

I found out this code is also possible.
1
2
3
4
5
6
7
8
#include <iostream>
template<class T>
T Add(T a,T b){return a+b;}
int main()
{
    std::cout.precision(50); 
std::cout<< (Add<long double>(-5.553892398912181215,8.238822124218157451258)) << '\n' ;
}


New question. Wich of the 2 do you prefer, and why?
> Which of the 2 do you prefer, and why?

Whichever makes code easier to read.

I/O manipulators manipulators are handy; they allow us to embed formatting information as part of the data that is sent to (received from) the stream. For instance:
1
2
std::cout << "number of items: " << std::setw(5) << nitems 
          << "   price: " << std::fixed << std::setprecision(2) << std::setw(6) << price << '\n' ;
Topic archived. No new replies allowed.