extra long numbers ?
Dec 29, 2014 at 8:24am UTC
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
Dec 29, 2014 at 8:52am UTC
Dec 29, 2014 at 9:55am UTC
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?
Dec 29, 2014 at 1:24pm UTC
> 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.