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