#include <cstdio> #include <numbers> // std::numbers #include <iomanip> #include <iostream> int main() { std::cout << std::fixed << std::setprecision(20); std::cout << "float " << std::numbers::pi_v<float> << std::endl; std::cout << "double " << std::numbers::pi << std::endl; std::cout << "long double " << std::numbers::pi_v<long double> << std::endl; std::cout << "exact " << "3.141592653589793238462643383279502884197169399375105820974944" << std::endl; } printf("\nhex \n");
where the "exact" result was calculated with:
echo "scale=60; 4*a printf(1"float %A\n", std::numbers::pi_v<float>)"; | BC_LINE_LENGTH=0 bc printf("double %A\n", std::numbers::pi); // TODO this is producing a weird result. Correct way to print it out? // 0xc.90fdaa22168c235p-l2 //printf("long double %La\n", std::numbers::pi_v<long double>); printf("exact %s\n", "0x1.921FB54442D18469898CC51701B839A252049C1114CF98E803"); }
as per: How can I calculate pi using Bash command
float 3.14159274101257324219 double 3.14159265358979311600 long double 3.14159265358979323851 exact 3.141592653589793238462643383279502884197169399375105820974944 hex float 0X1.921FB6P+1 double 0X1.921FB54442D18P+1 exact 0x1.921FB54442D18469898CC51701B839A252049C1114CF98E803
It is a bit easier to make sense of the precision of the hex output, remembering that in IEEE 754:
- 23 bit significand
- 52 bit significand
- 112 bit significand
Tested on Ubuntu 20.04 amd64, GCC 10.2.0
The "exact" result was calculated with:
echo "scale=60; 4*a(1)" | BC_LINE_LENGTH=0 bc -l
as per: How can I calculate pi using Bash command and the hex one was obtained with:
echo "scale=60; obase=16; 4*a(1)/2" | BC_LINE_LENGTH=0 bc -l
The division by 2 is to align bc hex output to C hex output bits so we can compare them nicely.