fork download
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. template <typename T> double sgn(T val) {
  6. return double((T(0) < val) - (val < T(0)))/(val == val);
  7. }
  8.  
  9. int main() {
  10. cout << "sgn(-1) : " << sgn(-1) << endl;
  11. cout << "sgn(0) : " << sgn(0) << endl;
  12. cout << "sgn(1) : " << sgn(1) << endl;
  13. cout << "sgn(-INFINITY): " << sgn(-INFINITY) << endl;
  14. cout << "sgn(-1.0) : " << sgn(-1.0) << endl;
  15. cout << "sgn(-0.0) : " << sgn(-0.0) << endl;
  16. cout << "sgn(0.0) : " << sgn(+0.0) << endl;
  17. cout << "sgn(+1) : " << sgn(+1.0) << endl;
  18. cout << "sgn(INFINITY) : " << sgn(+INFINITY) << endl;
  19. cout << "sgn(NAN) : " << sgn(NAN) << endl;
  20. cout << "sgn(-NAN) : " << sgn(-NAN) << endl;
  21. return 0;
  22. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
sgn(-1)       : -1
sgn(0)        : 0
sgn(1)        : 1
sgn(-INFINITY): -1
sgn(-1.0)     : -1
sgn(-0.0)     : 0
sgn(0.0)      : 0
sgn(+1)       : 1
sgn(INFINITY) : 1
sgn(NAN)      : -nan
sgn(-NAN)     : -nan