tlx
Loading...
Searching...
No Matches
sgn.hpp
Go to the documentation of this file.
1/*******************************************************************************
2 * tlx/math/sgn.hpp
3 *
4 * sgn() return the signum (-1, 0, +1) of a value.
5 *
6 * Part of tlx - http://panthema.net/tlx
7 *
8 * Copyright (C) 2018 Timo Bingmann <tb@panthema.net>
9 *
10 * All rights reserved. Published under the Boost Software License, Version 1.0
11 ******************************************************************************/
12
13#ifndef TLX_MATH_SGN_HEADER
14#define TLX_MATH_SGN_HEADER
15
16namespace tlx {
17
18//! \addtogroup tlx_math
19//! \{
20
21/******************************************************************************/
22//! sgn() - signum
23
24//! return the signum (-1, 0, +1) of a value.
25template <typename T>
26int sgn(const T& val) {
27 // from https://stackoverflow.com/questions/1903954
28 return (T(0) < val) - (val < T(0));
29}
30
31//! \}
32
33} // namespace tlx
34
35#endif // !TLX_MATH_SGN_HEADER
36
37/******************************************************************************/
int sgn(const T &val)
sgn() - signum
Definition: sgn.hpp:26