std::gcd(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… 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 type std::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