std::gcd(3)
π NAME π
std::gcd - std::gcd β yes bestie, that's the whole NAME line, and it's giving minimalist icon behavior π
. This is the std::gcd(3) page from the C++ Standard Libary manual, and it's here to make greatest-common-divisor math feel like a main character moment π.
πΈ Synopsis πΈ
Defined in header <numeric>
template< class M, class N > (since C++17)
constexpr std::common_type_t<M, N> gcd( M m, N n );
It computes the greatest common divisor of the integers m and n.
If either M or N is not an integer type, or if either is (possibly cv-qualified) bool, the program is ill-formed.
If either|m|or|n|is not representable as a value of typestd::common_type_t<M, N>, the behavior is undefined. π
π Parameters π
m,nβ integer values
π Return value π
If both m and n are zero, returns zero. Otherwise, returns the greatest common divisor of |m| and |n|.
π¦ Exceptions π¦
Throws no exceptions. Not a single one. The drama-free icon we deserve β¨.
π Notes π
Feature-test macro deets, because the standard library loves receipts π§Ύ:
- Feature-test macro:
__cpp_lib_gcd_lcm - Value:
201606L - Std:
(C++17) - Feature:
std::gcd,std::lcm
π Example π
This is the code, bestie β run it and watch the static_asserts hit:
// Run this code
#include <numeric>
int main()
{
constexpr int p{2 * 2 * 3};
constexpr int q{2 * 3 * 3};
static_assert(2 * 3 == std::gcd(p, q));
static_assert(std::gcd( 6, 10) == 2);
static_assert(std::gcd( 6, -10) == 2);
static_assert(std::gcd(-6, -10) == 2);
static_assert(std::gcd( 24, 0) == 24);
static_assert(std::gcd(-24, 0) == 24);
}
π See also π
lcmβ computes the least common multiple of two integers (C++17) (function template)
mwah π source/version: http://cppreference.com β dated 2024.06.10 β page std::gcd(3), signing off with a compile-time kiss π«Άβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms