English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Heres the program:

#include
using namespace std;

int main()
{
long double pi=4.00;
bool op=false;

for (long double n=3.00;n<1000000.00;n+=2.00)
{
if (op==true)
{pi+=4.00/n; op=false;cout << pi << endl;}
else
{pi-=4.00/n; op=true; cout << pi << 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

2007-03-02 12:32:43 · 3 answers · asked by Pandu 2 in Computers & Internet Programming & Design

3 answers

Use iomanip.h header file and use std::setprecision(x), where x is the number of digits.

For example:
std::cout << "Value is " << std::setprecision(19) << x << std::endl;

2007-03-02 12:57:10 · answer #1 · answered by Rainmaker 2 · 1 0

Include and set the flags/precision. For example, this will display 8 digits and set the width to 20 places. also right justifies it.

cout < << setprecision(8)
<< setw(20)
<< myValue
<< resetiosflags(ios::right)
<< endl;

2007-03-02 20:59:10 · answer #2 · answered by BigRez 6 · 1 0

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.

2007-03-02 20:39:34 · answer #3 · answered by csanon 6 · 2 0

fedest.com, questions and answers