tetl
0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
gcd.hpp
Go to the documentation of this file.
1
// SPDX-License-Identifier: BSL-1.0
2
// SPDX-FileCopyrightText: Copyright (C) 2019 Tobias Hienzsch
3
#
ifndef
TETL_NUMERIC_GCD_HPP
4
#
define
TETL_NUMERIC_GCD_HPP
5
6
#
include
<
etl
/
_type_traits
/
common_type
.
hpp
>
7
8
namespace
etl
{
9
10
/// \brief Computes the greatest common divisor of the integers m and n.
11
///
12
/// \returns If both m and n are zero, returns zero. Otherwise, returns the
13
/// greatest common divisor of |m| and |n|.
14
///
15
/// \ingroup numeric
16
template
<
typename
M,
typename
N>
17
[[nodiscard]]
constexpr
auto
gcd
(M m, N n)
noexcept
->
etl
::common_type_t<M, N>
18
{
19
using
R =
etl
::common_type_t<M, N>;
20
21
R a =
static_cast
<R>(m);
22
R b =
static_cast
<R>(n);
23
24
while
(b != 0) {
25
auto
const
r =
static_cast
<R>(a % b);
26
a = b;
27
b = r;
28
}
29
30
return
a;
// If both inputs were 0, this is 0.
31
}
32
33
}
// namespace etl
34
35
#
endif
// TETL_NUMERIC_GCD_HPP
etl::gcd
constexpr auto gcd(M m, N n) noexcept -> etl::common_type_t< M, N >
Computes the greatest common divisor of the integers m and n.
Definition
gcd.hpp:17
etl
Definition
adjacent_find.hpp:9
include
etl
_numeric
gcd.hpp
Generated on Sun Sep 7 2025 19:15:15 for tetl by
1.9.8