Skip to content

Commit

Permalink
Implement ratio_gcd (which is, yes, used by Chrono)
Browse files Browse the repository at this point in the history
  • Loading branch information
pdimov committed Oct 13, 2023
1 parent 00a69ca commit 4d35c90
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 7 deletions.
11 changes: 4 additions & 7 deletions include/boost/ratio/ratio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,17 @@ time2_demo contained this comment:
#define BOOST_RATIO_RATIO_HPP

#include <boost/ratio/ratio_fwd.hpp>
#include <boost/ratio/detail/gcd_lcm.hpp>

namespace boost
{

/*
template <class R1, class R2>
struct ratio_gcd :
ratio<mpl::gcd_c<boost::intmax_t, R1::num, R2::num>::value,
mpl::lcm_c<boost::intmax_t, R1::den, R2::den>::value>::type
template <class R1, class R2> struct ratio_gcd: ratio<
ratio_detail::gcd<R1::num, R2::num>::value,
ratio_detail::lcm<R1::den, R2::den>::value>::type
{
};
*/

} // namespace boost


#endif // BOOST_RATIO_RATIO_HPP
1 change: 1 addition & 0 deletions test/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ compile ratio_arithmetic/ratio_add_pass.cpp ;
compile ratio_arithmetic/ratio_subtract_pass.cpp ;
compile ratio_arithmetic/ratio_multiply_pass.cpp ;
compile ratio_arithmetic/ratio_divide_pass.cpp ;
compile ratio_arithmetic/ratio_gcd_pass.cpp ;
compile-fail ratio_arithmetic/ratio_add_fail.cpp ;
compile-fail ratio_arithmetic/ratio_add_2_fail.cpp ;
#compile-fail ratio_arithmetic/ratio_add_3_fail.cpp ;
Expand Down
65 changes: 65 additions & 0 deletions test/ratio_arithmetic/ratio_gcd_pass.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2023 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <boost/ratio/ratio.hpp>

#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)

void test()
{
{
typedef boost::ratio<1, 1> R1;
typedef boost::ratio<1, 1> R2;
typedef boost::ratio_gcd<R1, R2> R;
STATIC_ASSERT(R::num == 1 && R::den == 1);
}
{
typedef boost::ratio<1, 2> R1;
typedef boost::ratio<1, 1> R2;
typedef boost::ratio_gcd<R1, R2> R;
STATIC_ASSERT(R::num == 1 && R::den == 2);
}
{
typedef boost::ratio<-1, 2> R1;
typedef boost::ratio<1, 1> R2;
typedef boost::ratio_gcd<R1, R2> R;
STATIC_ASSERT(R::num == 1 && R::den == 2);
}
{
typedef boost::ratio<1, -2> R1;
typedef boost::ratio<1, 1> R2;
typedef boost::ratio_gcd<R1, R2> R;
STATIC_ASSERT(R::num == 1 && R::den == 2);
}
{
typedef boost::ratio<1, 2> R1;
typedef boost::ratio<-1, 1> R2;
typedef boost::ratio_gcd<R1, R2> R;
STATIC_ASSERT(R::num == 1 && R::den == 2);
}
{
typedef boost::ratio<1, 2> R1;
typedef boost::ratio<1, -1> R2;
typedef boost::ratio_gcd<R1, R2> R;
STATIC_ASSERT(R::num == 1 && R::den == 2);
}
{
typedef boost::ratio<1, 2> R1;
typedef boost::ratio<1, 3> R2;
typedef boost::ratio_gcd<R1, R2> R;
STATIC_ASSERT(R::num == 1 && R::den == 6);
}
{
typedef boost::ratio<7, 13> R1;
typedef boost::ratio<11, 13> R2;
typedef boost::ratio_gcd<R1, R2> R;
STATIC_ASSERT(R::num == 1 && R::den == 13);
}
{
typedef boost::ratio<21, 55> R1;
typedef boost::ratio<14, 25> R2;
typedef boost::ratio_gcd<R1, R2> R;
STATIC_ASSERT(R::num == 7 && R::den == 275);
}
}

0 comments on commit 4d35c90

Please sign in to comment.