Iros
 
Loading...
Searching...
No Matches
lcm.h
Go to the documentation of this file.
1#pragma once
2
3#include "di/math/gcd.h"
4
5namespace di::math {
6namespace detail {
7 struct LcmFunction {
8 template<concepts::Integer T>
9 constexpr auto operator()(T m) const {
10 return m;
11 }
12
13 template<concepts::Integer T, concepts::Integer U>
14 constexpr auto operator()(T m, U n) const {
15 using R = meta::CommonType<T, U>;
16 if (m == 0 || n == 0) {
17 return R(0);
18 }
19
20 using UR = meta::MakeUnsigned<R>;
21 UR a = math::abs_unsigned(m);
22 UR b = math::abs_unsigned(n);
23
24 return R(a * (b / gcd(a, b)));
25 }
26
27 template<concepts::Integer T, concepts::Integer... Rest>
28 constexpr auto operator()(T m, Rest... rest) const {
29 return (*this)(m, (*this)(rest...));
30 }
31 };
32}
33
34constexpr inline auto lcm = detail::LcmFunction {};
35}
36
37namespace di {
38using math::lcm;
39}
Definition language.h:215
Definition abs.h:11
Definition abs.h:10
constexpr auto lcm
Definition lcm.h:34
constexpr auto abs_unsigned
Definition abs_unsigned.h:26
constexpr auto gcd
Definition gcd.h:44
detail::CommonTypeHelper< Types... >::Type CommonType
Definition common.h:62
detail::MakeUnsignedHelper< RemoveCV< T > >::Type MakeUnsigned
Definition language.h:362
Definition zstring_parser.h:9
constexpr auto operator()(T m, U n) const
Definition lcm.h:14
constexpr auto operator()(T m, Rest... rest) const
Definition lcm.h:28
constexpr auto operator()(T m) const
Definition lcm.h:9