WPILibC++ 2023.4.3
round.hpp
Go to the documentation of this file.
1/*################################################################################
2 ##
3 ## Copyright (C) 2016-2022 Keith O'Hara
4 ##
5 ## This file is part of the GCE-Math C++ library.
6 ##
7 ## Licensed under the Apache License, Version 2.0 (the "License");
8 ## you may not use this file except in compliance with the License.
9 ## You may obtain a copy of the License at
10 ##
11 ## http://www.apache.org/licenses/LICENSE-2.0
12 ##
13 ## Unless required by applicable law or agreed to in writing, software
14 ## distributed under the License is distributed on an "AS IS" BASIS,
15 ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 ## See the License for the specific language governing permissions and
17 ## limitations under the License.
18 ##
19 ################################################################################*/
20
21#ifndef _gcem_round_HPP
22#define _gcem_round_HPP
23
24namespace internal
25{
26
27template<typename T>
28constexpr
29T
30round_int(const T x)
31noexcept
32{
33 return( abs(x - internal::floor_check(x)) >= T(0.5) ? \
34 // if
35 internal::floor_check(x) + sgn(x) : \
36 // else
38}
39
40template<typename T>
41constexpr
42T
44noexcept
45{
46 return x;
47}
48
49template<>
50constexpr
51float
52round_check_internal<float>(const float x)
53noexcept
54{
55 return( abs(x) >= 8388608.f ? \
56 // if
57 x : \
58 //else
59 round_int(x) );
60}
61
62template<>
63constexpr
64double
65round_check_internal<double>(const double x)
66noexcept
67{
68 return( abs(x) >= 4503599627370496. ? \
69 // if
70 x : \
71 // else
72 round_int(x) );
73}
74
75template<>
76constexpr
77long double
78round_check_internal<long double>(const long double x)
79noexcept
80{
81 return( abs(x) >= 9223372036854775808.l ? \
82 // if
83 x : \
84 // else
85 round_int(x) );
86}
87
88template<typename T>
89constexpr
90T
91round_check(const T x)
92noexcept
93{
94 return( // NaN check
95 is_nan(x) ? \
96 GCLIM<T>::quiet_NaN() :
97 // +/- infinite
98 !is_finite(x) ? \
99 x :
100 // signed-zero cases
101 GCLIM<T>::min() > abs(x) ? \
102 x :
103 // else
104 sgn(x) * round_check_internal(abs(x)) );
105}
106
107}
108
109/**
110 * Compile-time round function
111 *
112 * @param x a real-valued input.
113 * @return computes the rounding value of the input.
114 */
115
116template<typename T>
117constexpr
118return_t<T>
119round(const T x)
120noexcept
121{
122 return internal::round_check( static_cast<return_t<T>>(x) );
123}
124
125#endif
UnitType abs(const UnitType x) noexcept
Compute absolute value.
Definition: math.h:721
constexpr common_t< T1, T2 > min(const T1 x, const T2 y) noexcept
Compile-time pairwise minimum function.
Definition: min.hpp:35
Definition: Eigen_Colamd.h:50
constexpr bool is_nan(const T x) noexcept
Definition: is_nan.hpp:36
constexpr float round_check_internal< float >(const float x) noexcept
Definition: round.hpp:52
constexpr long double round_check_internal< long double >(const long double x) noexcept
Definition: round.hpp:78
constexpr bool is_finite(const T x) noexcept
Definition: is_finite.hpp:34
constexpr T round_int(const T x) noexcept
Definition: round.hpp:30
constexpr T round_check(const T x) noexcept
Definition: round.hpp:91
constexpr double round_check_internal< double >(const double x) noexcept
Definition: round.hpp:65
constexpr T floor_check(const T x) noexcept
Definition: floor.hpp:96
constexpr T round_check_internal(const T x) noexcept
Definition: round.hpp:43
constexpr return_t< T > round(const T x) noexcept
Compile-time round function.
Definition: round.hpp:119
constexpr int sgn(const T x) noexcept
Compile-time sign function.
Definition: sgn.hpp:34