WPILibC++ 2023.4.3-108-ge5452e3
lcm.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_lcm_HPP
22#define _gcem_lcm_HPP
23
24namespace internal
25{
26
27template<typename T>
28constexpr
29T
30lcm_compute(const T a, const T b)
31noexcept
32{
33 return abs(a * (b / gcd(a,b)));
34}
35
36template<typename T1, typename T2, typename TC = common_t<T1,T2>>
37constexpr
38TC
39lcm_type_check(const T1 a, const T2 b)
40noexcept
41{
42 return lcm_compute(static_cast<TC>(a),static_cast<TC>(b));
43}
44
45}
46
47/**
48 * Compile-time least common multiple (LCM) function
49 *
50 * @param a integral-valued input.
51 * @param b integral-valued input.
52 * @return the least common multiple between integers \c a and \c b using the representation \f[ \text{lcm}(a,b) = \dfrac{| a b |}{\text{gcd}(a,b)} \f]
53 * where \f$ \text{gcd}(a,b) \f$ denotes the greatest common divisor between \f$ a \f$ and \f$ b \f$.
54 */
55
56template<typename T1, typename T2>
57constexpr
58common_t<T1,T2>
59lcm(const T1 a, const T2 b)
60noexcept
61{
63}
64
65#endif
constexpr common_t< T1, T2 > gcd(const T1 a, const T2 b) noexcept
Compile-time greatest common divisor (GCD) function.
Definition: gcd.hpp:76
UnitType abs(const UnitType x) noexcept
Compute absolute value.
Definition: math.h:721
constexpr common_t< T1, T2 > lcm(const T1 a, const T2 b) noexcept
Compile-time least common multiple (LCM) function.
Definition: lcm.hpp:59
Definition: Eigen_Colamd.h:50
constexpr T lcm_compute(const T a, const T b) noexcept
Definition: lcm.hpp:30
constexpr TC lcm_type_check(const T1 a, const T2 b) noexcept
Definition: lcm.hpp:39
b
Definition: data.h:44