I teach myself C ++ and this practice question , he asks to write code that can calculate PI up to β 30 digits. I found out that double / long double are 16 digits that exactly match my computer.
I think the lesson of this question is to be able to calculate accuracy beyond what is available. So how do I do this? Is it possible?
my code to calculate Pi now
#include "stdafx.h" #include <iostream> #include <math.h> #include <iomanip> using namespace std; int main(){ double pi; pi = 4*atan(1.0); cout<<setprecision(30)<<pi; return 0; }
The output is 16 digits, and for comparison - from pi to 30 digits.
3.1415926535897931 3.141592653589793238462643383279
Any suggestions for improving accuracy or is it something that doesn't ever matter? Alternatively, if there is another lesson, you think I should study here, do not hesitate to offer it. Thanks!
source share