WPILibC++ 2023.4.3
ceil.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_ceil_HPP
22#define _gcem_ceil_HPP
23
24namespace internal
25{
26
27template<typename T>
28constexpr
29int
30ceil_resid(const T x, const T x_whole)
31noexcept
32{
33 return( (x > T(0)) && (x > x_whole) );
34}
35
36template<typename T>
37constexpr
38T
39ceil_int(const T x, const T x_whole)
40noexcept
41{
42 return( x_whole + static_cast<T>(ceil_resid(x,x_whole)) );
43}
44
45template<typename T>
46constexpr
47T
49noexcept
50{
51 return x;
52}
53
54template<>
55constexpr
56float
57ceil_check_internal<float>(const float x)
58noexcept
59{
60 return( abs(x) >= 8388608.f ? \
61 // if
62 x : \
63 // else
64 ceil_int(x, float(static_cast<int>(x))) );
65}
66
67template<>
68constexpr
69double
70ceil_check_internal<double>(const double x)
71noexcept
72{
73 return( abs(x) >= 4503599627370496. ? \
74 // if
75 x : \
76 // else
77 ceil_int(x, double(static_cast<llint_t>(x))) );
78}
79
80template<>
81constexpr
82long double
83ceil_check_internal<long double>(const long double x)
84noexcept
85{
86 return( abs(x) >= 9223372036854775808.l ? \
87 // if
88 x : \
89 // else
90 ceil_int(x, ((long double)static_cast<ullint_t>(abs(x))) * sgn(x)) );
91}
92
93template<typename T>
94constexpr
95T
96ceil_check(const T x)
97noexcept
98{
99 return( // NaN check
100 is_nan(x) ? \
101 GCLIM<T>::quiet_NaN() :
102 // +/- infinite
103 !is_finite(x) ? \
104 x :
105 // signed-zero cases
106 GCLIM<T>::min() > abs(x) ? \
107 x :
108 // else
110}
111
112}
113
114/**
115 * Compile-time ceil function
116 *
117 * @param x a real-valued input.
118 * @return computes the ceiling-value of the input.
119 */
120
121template<typename T>
122constexpr
123return_t<T>
124ceil(const T x)
125noexcept
126{
127 return internal::ceil_check( static_cast<return_t<T>>(x) );
128}
129
130#endif
constexpr return_t< T > ceil(const T x) noexcept
Compile-time ceil function.
Definition: ceil.hpp:124
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
unsigned long long int ullint_t
Definition: gcem_options.hpp:69
long long int llint_t
Definition: gcem_options.hpp:71
Definition: Eigen_Colamd.h:50
constexpr T ceil_int(const T x, const T x_whole) noexcept
Definition: ceil.hpp:39
constexpr int ceil_resid(const T x, const T x_whole) noexcept
Definition: ceil.hpp:30
constexpr bool is_nan(const T x) noexcept
Definition: is_nan.hpp:36
constexpr T ceil_check_internal(const T x) noexcept
Definition: ceil.hpp:48
constexpr long double ceil_check_internal< long double >(const long double x) noexcept
Definition: ceil.hpp:83
constexpr float ceil_check_internal< float >(const float x) noexcept
Definition: ceil.hpp:57
constexpr bool is_finite(const T x) noexcept
Definition: is_finite.hpp:34
constexpr double ceil_check_internal< double >(const double x) noexcept
Definition: ceil.hpp:70
constexpr T ceil_check(const T x) noexcept
Definition: ceil.hpp:96
constexpr int sgn(const T x) noexcept
Compile-time sign function.
Definition: sgn.hpp:34