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