Function lcm

Synopsis

#include "breeze/mathematics/lcm.hpp"

template <typename T>
constexpr T lcm(T a, T b)

Description

Returns the least common multiple of two integers.

If T is not an integral type the program is ill-formed. If the least common multiple of |a| and |b| is not representable as a value of type T, the behavior is undefined.

Returns
If either a or b is zero, returns zero. Otherwise it returns the least common multiple of |a| and |b|.
Note
This template diverges from the C++17 specification for the same reason why breeze::gcd() does.
See
gcd().

Mentioned in

Source

Lines 17-42 in breeze/mathematics/brz/lcm.tpp. Line 41 in breeze/mathematics/lcm.hpp.

template< typename T >
constexpr T
lcm( T a, T b )
{
    static_assert( std::is_integral< T >::value, "T must be integral" ) ;

    using gcd_lcm_private::absolute_value ;

    T                   result = 0 ;

    if ( a != 0 && b != 0 ) {
        auto const          factor1 =
            absolute_value( a ) / breeze::gcd( a, b ) ;
        auto const          factor2 = absolute_value( b ) ;

        //      We renounce to using BREEZE_PREVENT_MACRO_EXPANSION
        //      here, for readability.
        // -------------------------------------------------------------------
        BREEZE_ASSERT( factor1 <=
                ( ( std::numeric_limits< T >::max )() / factor2 ) ) ;

        result = factor1 * factor2 ;
    }

    return result ;
}