Method 'setprecision()' does not work!

In following code setprecision(11) does not work at all, it prints 41.3328 - not 11 digits !What is going on?

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
double f(double x){
return x*x;}

double rectangle_method( double (*f)(double), double a, double b,
unsigned int n)
{
if (n == 0)
return 0.0;
double sum = 0.0;
double h = (b - a) / n;
for (unsigned int i = 0; i < n; i++)
sum += f(a + (i + 0.5) * h);
return h * sum;
}
int main(){
double x;
cout<<setprecision(11) <<rectangle_method(f,1,5,100)<<endl;
return 0;
}
Last edited on
In following code setprecision(11) does not work at all, it prints 41.3328 - not 11 digits !What is going on?


cout<< fixed << setprecision(11) << rectangle_method(f,1,5,100)<< endl;
Topic archived. No new replies allowed.