fork(1) download
  1. #include <cstdint>
  2.  
  3. constexpr double pow2(std::int16_t e)
  4. {
  5. return e == 0 ? 1. :
  6. e > 0 ? 2. * pow2(std::int16_t(e - 1)) :
  7. 0.5 * pow2(std::int16_t(e + 1));
  8. }
  9.  
  10. static_assert(pow2(0) == 1., "");
  11. static_assert(pow2(1) == 2., "");
  12. static_assert(pow2(10) == 1024., "");
  13. static_assert(pow2(-1) == 0.5, "");
  14. static_assert(pow2(-2) == 0.25, "");
  15.  
  16. template<bool S , std::int16_t E , std::uint32_t M>
  17. struct number
  18. {
  19. static constexpr const bool sign = S;
  20. static constexpr const std::int16_t exponent = E;
  21. static constexpr const std::uint32_t mantissa = M;
  22.  
  23. static constexpr const double value = (sign ? -1. : 1.) * M * pow2(E);
  24. };
  25.  
  26. static_assert(number<false, 0, 42>::value == 42., "");
  27. static_assert(number<false, 1, 3>::value == 6., "");
  28. static_assert(number<false, 10, 2>::value == 2048., "");
  29. static_assert(number<false, -1, 2>::value == 1., "");
  30. static_assert(number<false, -2, 1>::value == 0.25, "");
  31.  
  32.  
  33. int main() {
  34. return 0;
  35. }
  36.  
Success #stdin #stdout 0s 3292KB
stdin
Standard input is empty
stdout
Standard output is empty