这里的问题甚至是long double
精度有限。考虑这个 ( C++11
)
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;
int main() {
cout.precision(51);
std::string pi("3.14159265358979323846264338327950288419716939937510");
cout << pi << endl;
cout << stold(pi) << endl;
cout << M_PIl << endl; /// The constant from <math.h>
}
输出
3.14159265358979323846264338327950288419716939937510
3.14159265358979323851280895940618620443274267017841
^ value changes from here (18th decimal place)
3.14159265358979323851280895940618620443274267017841