mirror of
https://github.com/boostorg/ratio.git
synced 2025-05-09 23:24:01 +00:00
Add ratio/detail/gcd_lcm.hpp
This commit is contained in:
parent
05a053c9a4
commit
00a69ca14d
53
include/boost/ratio/detail/gcd_lcm.hpp
Normal file
53
include/boost/ratio/detail/gcd_lcm.hpp
Normal file
@ -0,0 +1,53 @@
|
||||
#ifndef BOOST_RATIO_DETAIL_GCD_LCM_HPP
|
||||
#define BOOST_RATIO_DETAIL_GCD_LCM_HPP
|
||||
|
||||
// Copyright 2023 Peter Dimov
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// https://www.boost.org/LICENSE_1_0.txt
|
||||
|
||||
#include <type_traits>
|
||||
#include <cstdint>
|
||||
|
||||
namespace boost
|
||||
{
|
||||
namespace ratio_detail
|
||||
{
|
||||
|
||||
template<std::intmax_t A> struct abs_: std::integral_constant<std::intmax_t, A < 0? -A: A>
|
||||
{
|
||||
};
|
||||
|
||||
template<> struct abs_<INTMAX_MIN>: std::integral_constant<std::intmax_t, INTMAX_MIN>
|
||||
{
|
||||
};
|
||||
|
||||
template<std::intmax_t A, std::intmax_t B> struct gcd_: public gcd_<B, A % B>
|
||||
{
|
||||
};
|
||||
|
||||
template<std::intmax_t A> struct gcd_<A, 0>: std::integral_constant<std::intmax_t, A>
|
||||
{
|
||||
};
|
||||
|
||||
template<std::intmax_t A, std::intmax_t B> struct lcm_: std::integral_constant<std::intmax_t, (A / gcd_<A, B>::value) * B>
|
||||
{
|
||||
};
|
||||
|
||||
template<> struct lcm_<0, 0>: std::integral_constant<std::intmax_t, 0>
|
||||
{
|
||||
};
|
||||
|
||||
//
|
||||
|
||||
template<std::intmax_t A, std::intmax_t B> struct gcd: abs_< gcd_<A, B>::value >
|
||||
{
|
||||
};
|
||||
|
||||
template<std::intmax_t A, std::intmax_t B> struct lcm: abs_< lcm_<A, B>::value >
|
||||
{
|
||||
};
|
||||
|
||||
} // namespace ratio_detail
|
||||
} // namespace boost
|
||||
|
||||
#endif // BOOST_RATIO_DETAIL_GCD_LCM_HPP
|
@ -53,6 +53,7 @@ compile ratio_comparison/ratio_greater_equal_pass.cpp ;
|
||||
# test-suite "ratio_detail"
|
||||
|
||||
run ratio_detail/is_evenly_divisible_by.cpp ;
|
||||
run ratio_detail/gcd_lcm.cpp ;
|
||||
|
||||
# test-suite "examples"
|
||||
|
||||
|
33
test/ratio_detail/gcd_lcm.cpp
Normal file
33
test/ratio_detail/gcd_lcm.cpp
Normal file
@ -0,0 +1,33 @@
|
||||
// Copyright 2023 Peter Dimov
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// https://www.boost.org/LICENSE_1_0.txt
|
||||
|
||||
#include <boost/ratio/detail/gcd_lcm.hpp>
|
||||
#include <boost/core/lightweight_test_trait.hpp>
|
||||
#include <cstdint>
|
||||
|
||||
using boost::ratio_detail::gcd;
|
||||
using boost::ratio_detail::lcm;
|
||||
|
||||
int main()
|
||||
{
|
||||
BOOST_TEST_EQ( (gcd<1, 1>::value), 1 );
|
||||
BOOST_TEST_EQ( (gcd<1, -1>::value), 1 );
|
||||
BOOST_TEST_EQ( (gcd<2, 2>::value), 2 );
|
||||
BOOST_TEST_EQ( (gcd<-2, -2>::value), 2 );
|
||||
BOOST_TEST_EQ( (gcd<INTMAX_MAX, INTMAX_MAX>::value), INTMAX_MAX );
|
||||
BOOST_TEST_EQ( (gcd<INTMAX_MAX, -INTMAX_MAX>::value), INTMAX_MAX );
|
||||
BOOST_TEST_EQ( (gcd<-INTMAX_MAX, -INTMAX_MAX>::value), INTMAX_MAX );
|
||||
BOOST_TEST_EQ( (gcd<7*11, 11*13>::value), 11 );
|
||||
|
||||
BOOST_TEST_EQ( (lcm<1, 1>::value), 1 );
|
||||
BOOST_TEST_EQ( (lcm<1, -1>::value), 1 );
|
||||
BOOST_TEST_EQ( (lcm<2, 2>::value), 2 );
|
||||
BOOST_TEST_EQ( (lcm<-2, -2>::value), 2 );
|
||||
BOOST_TEST_EQ( (lcm<INTMAX_MAX, INTMAX_MAX>::value), INTMAX_MAX );
|
||||
BOOST_TEST_EQ( (lcm<INTMAX_MAX, -INTMAX_MAX>::value), INTMAX_MAX );
|
||||
BOOST_TEST_EQ( (lcm<-INTMAX_MAX, -INTMAX_MAX>::value), INTMAX_MAX );
|
||||
BOOST_TEST_EQ( (lcm<7*11, 11*13>::value), 7*11*13 );
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user