fork(7) download
  1. #include <cstdint>
  2. #include <limits>
  3.  
  4. template <typename T>
  5. constexpr double normalize (T value) {
  6. return value < 0
  7. ? -static_cast<double>(value) / std::numeric_limits<T>::min()
  8. : static_cast<double>(value) / std::numeric_limits<T>::max()
  9. ;
  10. }
  11.  
  12. int main () {
  13. // Test cases evaluated at compile time.
  14. static_assert(normalize(int16_t(32767)) == 1, "");
  15. static_assert(normalize(int16_t(0)) == 0, "");
  16. static_assert(normalize(int16_t(-32768)) == -1, "");
  17. static_assert(normalize(int16_t(-16384)) == -0.5, "");
  18. static_assert(normalize(uint16_t(65535)) == 1, "");
  19. static_assert(normalize(uint16_t(0)) == 0, "");
  20. }
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
Standard output is empty