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