Embedded Template Library 1.0
Loading...
Searching...
No Matches
lcm.h
Go to the documentation of this file.
1
2
3/******************************************************************************
4The MIT License(MIT)
5
6Embedded Template Library.
7https://github.com/ETLCPP/etl
8https://www.etlcpp.com
9
10Copyright(c) 2024 John Wellbelove
11
12Permission is hereby granted, free of charge, to any person obtaining a copy
13of this software and associated documentation files(the "Software"), to deal
14in the Software without restriction, including without limitation the rights
15to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16copies of the Software, and to permit persons to whom the Software is
17furnished to do so, subject to the following conditions :
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28SOFTWARE.
29******************************************************************************/
30
31#ifndef ETL_LCM_INCLUDED
32#define ETL_LCM_INCLUDED
33
34#include "type_traits.h"
35#include "absolute.h"
36#include "static_assert.h"
37#include "gcd.h"
38
39namespace etl
40{
41 //***************************************************************************
42 // Least Common Multiple.
43 // For unsigned types.
44 //***************************************************************************
45 template <typename T>
46 ETL_NODISCARD
47 ETL_CONSTEXPR14
49 lcm(T a, T b) ETL_NOEXCEPT
50 {
51 ETL_STATIC_ASSERT(etl::is_integral<T>::value, "Integral type required");
52
53 // Early termination: if either number is zero, the LCM is zero.
54 if (a == 0 || b == 0)
55 {
56 return 0;
57 }
58 else
59 {
60 return a * (b / gcd(a, b));
61 }
62 }
63
64 //***************************************************************************
65 // Least Common Multiple.
66 // For signed types.
67 //***************************************************************************
68 template <typename T>
69 ETL_NODISCARD
70 ETL_CONSTEXPR14
72 lcm(T a, T b) ETL_NOEXCEPT
73 {
74 ETL_STATIC_ASSERT(etl::is_integral<T>::value, "Integral type required");
75
76 typedef typename etl::make_unsigned<T>::type utype;
77
78 utype ua = etl::absolute_unsigned(a);
79 utype ub = etl::absolute_unsigned(b);
80
81 return static_cast<T>(lcm(ua, ub));
82 }
83
84#if ETL_USING_CPP11
85 #if ETL_HAS_INITIALIZER_LIST
86 //***************************************************************************
87 // Least Common Multiple.
88 // Non-recursive, using an initializer_list.
89 // Top level variadic function.
90 //***************************************************************************
91 template<typename T, typename... TRest>
92 ETL_NODISCARD
93 ETL_CONSTEXPR14
94 T lcm(T first, TRest... rest) ETL_NOEXCEPT
95 {
96 T result = first;
97
98 for (T value : {rest...})
99 {
100 result = lcm(result, value);
101
102 if (result == 0)
103 {
104 // Early termination: if the LCM is zero, it will remain zero
105 // no matter what other numbers are processed.
106 return 0;
107 }
108 }
109
110 return result;
111 }
112 #else
113 //***************************************************************************
114 // Least Common Multiple.
115 // Recursive
116 // Top level variadic function.
117 //***************************************************************************
118 template<typename T, typename... TRest>
119 ETL_NODISCARD
120 ETL_CONSTEXPR14
121 T lcm(T a, T b, TRest... rest) ETL_NOEXCEPT
122 {
123 T lcm_ab = lcm(a, b);
124
125 if (lcm_ab == 0)
126 {
127 // Early termination: if the LCM is zero, it will remain zero
128 // no matter what other numbers are processed.
129 return 0;
130 }
131 else
132 {
133 return lcm(lcm_ab, rest...);
134 }
135 }
136 #endif
137#endif
138}
139
140#endif
141
is_integral
Definition type_traits_generator.h:1001
make_unsigned
Definition type_traits_generator.h:1181
bitset_ext
Definition absolute.h:38
pair holds two objects of arbitrary type
Definition utility.h:164