WPILibC++ 2024.3.2
round.hpp
Go to the documentation of this file.
1/*################################################################################
2 ##
3 ## Copyright (C) 2016-2023 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
24#include <cmath>
25#include <type_traits>
26
27namespace gcem
28{
29
30namespace internal
31{
32
33template<typename T>
34constexpr
35T
36round_int(const T x)
37noexcept
38{
39 return( abs(x - internal::floor_check(x)) >= T(0.5) ? \
40 // if
41 internal::floor_check(x) + sgn(x) : \
42 // else
44}
45
46template<typename T>
47constexpr
48T
50noexcept
51{
52 return x;
53}
54
55template<>
56constexpr
57float
58round_check_internal<float>(const float x)
59noexcept
60{
61 return( abs(x) >= 8388608.f ? \
62 // if
63 x : \
64 //else
65 round_int(x) );
66}
67
68template<>
69constexpr
70double
71round_check_internal<double>(const double x)
72noexcept
73{
74 return( abs(x) >= 4503599627370496. ? \
75 // if
76 x : \
77 // else
78 round_int(x) );
79}
80
81template<>
82constexpr
83long double
84round_check_internal<long double>(const long double x)
85noexcept
86{
87 return( abs(x) >= 9223372036854775808.l ? \
88 // if
89 x : \
90 // else
91 round_int(x) );
92}
93
94template<typename T>
95constexpr
96T
97round_check(const T x)
98noexcept
99{
100 return( // NaN check
101 is_nan(x) ? \
103 // +/- infinite
104 !is_finite(x) ? \
105 x :
106 // signed-zero cases
107 GCLIM<T>::min() > abs(x) ? \
108 x :
109 // else
110 sgn(x) * round_check_internal(abs(x)) );
111}
112
113}
114
115/**
116 * Compile-time round function
117 *
118 * @param x a real-valued input.
119 * @return computes the rounding value of the input.
120 */
121
122template<typename T>
123constexpr
124return_t<T>
125round(const T x)
126noexcept
127{
129 return internal::round_check( static_cast<return_t<T>>(x) );
130 } else {
131 return std::round(x);
132 }
133}
134
135}
136
137#endif
constexpr FMT_INLINE auto is_constant_evaluated(bool default_value=false) noexcept -> bool
Definition: core.h:304
constexpr float round_check_internal< float >(const float x) noexcept
Definition: round.hpp:58
constexpr bool is_nan(const T x) noexcept
Definition: is_nan.hpp:39
constexpr long double round_check_internal< long double >(const long double x) noexcept
Definition: round.hpp:84
constexpr double round_check_internal< double >(const double x) noexcept
Definition: round.hpp:71
constexpr T floor_check(const T x) noexcept
Definition: floor.hpp:102
constexpr bool is_finite(const T x) noexcept
Definition: is_finite.hpp:37
constexpr T round_check_internal(const T x) noexcept
Definition: round.hpp:49
constexpr T round_int(const T x) noexcept
Definition: round.hpp:36
constexpr T round_check(const T x) noexcept
Definition: round.hpp:97
Definition: is_even.hpp:29
constexpr return_t< T > round(const T x) noexcept
Compile-time round function.
Definition: round.hpp:125
constexpr T abs(const T x) noexcept
Compile-time absolute value function.
Definition: abs.hpp:40
constexpr int sgn(const T x) noexcept
Compile-time sign function.
Definition: sgn.hpp:37
std::numeric_limits< T > GCLIM
Definition: gcem_options.hpp:74
typename std::conditional< std::is_integral< T >::value, double, T >::type return_t
Definition: gcem_options.hpp:77