Heres the program:
#include %26lt;iostream%26gt;
using namespace std;
int main()
{
long double pi=4.00;
bool op=false;
for (long double n=3.00;n%26lt;1000000.00;n+=2.00)
{
if (op==true)
{pi+=4.00/n; op=false;cout %26lt;%26lt; pi %26lt;%26lt; endl;}
else
{pi-=4.00/n; op=true; cout %26lt;%26lt; pi %26lt;%26lt; endl;}
}
system ("PAUSE");
return 0;
}
it returns more and more accurate approximations of pi, but it only returns 6 digits, doesnt long double have a precision of 19 digits??
i need it to output more digits after the decimal
C++: outputting a long double type variable gives only 6 digits?
Use iomanip.h header file and use std::setprecision(x), where x is the number of digits.
For example:
std::cout %26lt;%26lt; "Value is " %26lt;%26lt; std::setprecision(19) %26lt;%26lt; x %26lt;%26lt; std::endl;
Reply:Include %26lt;iomanip.h%26gt; and set the flags/precision. For example, this will display 8 digits and set the width to 20 places. also right justifies it.
cout %26lt;%26lt;setiosflags(ios::right)
%26lt;%26lt; setprecision(8)
%26lt;%26lt; setw(20)
%26lt;%26lt; myValue
%26lt;%26lt; resetiosflags(ios::right)
%26lt;%26lt; endl;
Reply:Sure, long double will have more. cout has a default precision of 6 only, and so only 6 is displayed.
Want more? Change it using the iomanip flags.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment