WPILibC++ 2024.3.2
fmod.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_fmod_HPP
22#define _gcem_fmod_HPP
23
24#include <cmath>
25#include <type_traits>
26
27namespace gcem
28{
29
30namespace internal
31{
32
33template<typename T>
34constexpr
35T
36fmod_check(const T x, const T y)
37noexcept
38{
39 return( // NaN check
40 any_nan(x, y) ? \
42 // +/- infinite
43 !all_finite(x, y) ? \
45 // else
46 x - trunc(x/y)*y );
47}
48
49template<typename T1, typename T2, typename TC = common_return_t<T1,T2>>
50constexpr
51TC
52fmod_type_check(const T1 x, const T2 y)
53noexcept
54{
55 return fmod_check(static_cast<TC>(x),static_cast<TC>(y));
56}
57
58}
59
60/**
61 * Compile-time remainder of division function
62 * @param x a real-valued input.
63 * @param y a real-valued input.
64 * @return computes the floating-point remainder of \f$ x / y \f$ (rounded towards zero) using \f[ \text{fmod}(x,y) = x - \text{trunc}(x/y) \times y \f]
65 */
66
67template<typename T1, typename T2>
68constexpr
69common_return_t<T1,T2>
70fmod(const T1 x, const T2 y)
71noexcept
72{
74 return internal::fmod_type_check(x,y);
75 } else {
76 return std::fmod(x, y);
77 }
78}
79
80}
81
82#endif
constexpr FMT_INLINE auto is_constant_evaluated(bool default_value=false) noexcept -> bool
Definition: core.h:304
constexpr bool all_finite(const T1 x, const T2 y) noexcept
Definition: is_finite.hpp:55
constexpr TC fmod_type_check(const T1 x, const T2 y) noexcept
Definition: fmod.hpp:52
constexpr T fmod_check(const T x, const T y) noexcept
Definition: fmod.hpp:36
constexpr bool any_nan(const T1 x, const T2 y) noexcept
Definition: is_nan.hpp:48
Definition: is_even.hpp:29
constexpr common_return_t< T1, T2 > fmod(const T1 x, const T2 y) noexcept
Compile-time remainder of division function.
Definition: fmod.hpp:70
constexpr return_t< T > trunc(const T x) noexcept
Compile-time trunc function.
Definition: trunc.hpp:121
std::numeric_limits< T > GCLIM
Definition: gcem_options.hpp:74