codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
#include <limits> template <typename T> T clamp(T x, T lower, T upper) { if (x < lower) return lower; if (x > upper) return upper; return x; } short normalized_float_to_short(float x) { return short(clamp(x, -1.0f, 1.0f) * std::numeric_limits<short>::max()); } // --- test --- #include <iostream> int main() { std::cout << normalized_float_to_short(-2.0f) << std::endl; std::cout << normalized_float_to_short(-1.0f) << std::endl; std::cout << normalized_float_to_short(-0.5f) << std::endl; std::cout << normalized_float_to_short(-0.1f) << std::endl; std::cout << normalized_float_to_short( 0.0f) << std::endl; std::cout << normalized_float_to_short( 0.1f) << std::endl; std::cout << normalized_float_to_short( 0.5f) << std::endl; std::cout << normalized_float_to_short( 1.0f) << std::endl; std::cout << normalized_float_to_short(10.0f) << std::endl; }
Private
[
?
]
Run code
Submit