constexpr_math.h
Go to the documentation of this file.
1 /**
2  * @file lardataalg/Utilities/constexpr_math.h
3  * @brief Mathematical functions that C++ standard doesn't require `constexpr`.
4  * @author Gianluca Petrillo
5  * @date January 11, 2019
6  *
7  * This is a header-only library.
8  */
9 
10 #ifndef LARDATAALG_UTILITIES_CONSTEXPR_MATH_H
11 #define LARDATAALG_UTILITIES_CONSTEXPR_MATH_H
12 
13 namespace util {
14 
15  // --- BEGIN simple mathematical functions -----------------------------------
16  /// @name simple mathematical functions
17  /// @{
18 
19  /**
20  * @brief Returns the absolute value of the argument.
21  * @tparam T type of the argument
22  * @param v value to be processed
23  * @return the absolute value of v
24  *
25  * If the value `v` is negative, its opposite is returned.
26  * Note that this implementation does not work with data types that are not
27  * comparable (like `std::complex`).
28  *
29  *
30  * Requirements
31  * -------------
32  *
33  * * constexpr construction of a `T` value from the literal `0`
34  * * `operator- (T) constexpr`
35  * * `operator< (T, T) constexpr` convertible to `bool`
36  *
37  */
38  // this bizarre implementation is supposed to guarantee that `abs(-0)` is `0`
39  template <typename T>
40  constexpr auto abs(T v) { return (-T(0) < v)? v: -v; }
41 
42  /// @}
43 
44  // --- END simple mathematical functions -------------------------------------
45 
46 } // namespace util
47 
48 
49 #endif // LARDATAALG_UTILITIES_CONSTEXPR_MATH_H
Namespace for general, non-LArSoft-specific utilities.
constexpr auto abs(T v)
Returns the absolute value of the argument.