fork download
  1. /*
  2. Outputting the number of decimal significant figures supported by each floating point type in C++.
  3. Author: Peter R. Bloomfield.
  4. Web: avidinsight.uk
  5. */
  6.  
  7. #include <iostream>
  8. #include <limits>
  9. #include <cmath>
  10. using namespace std;
  11.  
  12. template <typename T_ty>
  13. void test()
  14. {
  15. int radix = numeric_limits<T_ty>::radix;
  16. int mantissa = numeric_limits<T_ty>::digits;
  17. int figures = static_cast<int>( std::ceil(
  18. mantissa * std::log(radix) / std::log(10.0)
  19. ));
  20.  
  21. cout << " Mantissa contains " << mantissa << " base " << radix << " digits" << endl;
  22. cout << " Significant decimal figures = " << figures << endl;
  23. cout << endl;
  24. }
  25.  
  26. int main() {
  27. cout << "float:" << endl;
  28. test<float>();
  29.  
  30. cout << "double:" << endl;
  31. test<double>();
  32.  
  33. cout << "long double:" << endl;
  34. test<long double>();
  35. return 0;
  36. }
Success #stdin #stdout 0s 3416KB
stdin
Standard input is empty
stdout
float:
 Mantissa contains 24 base 2 digits
 Significant decimal figures = 8

double:
 Mantissa contains 53 base 2 digits
 Significant decimal figures = 16

long double:
 Mantissa contains 64 base 2 digits
 Significant decimal figures = 20