Boost Users :

Subject: Re: [Boost-users] [rational] proposed gcd and lcm for rational
From: Howard Hinnant (howard.hinnant_at_[hidden])
Date: 2011-07-21 19:56:40

There is one use case for gcd of rationals in C++11:

[time.traits.specializations]/p1-2:

```---
template <class Rep1, class Period1, class Rep2, class Period2>
struct common_type<chrono::duration<Rep1, Period1>, chrono::duration<Rep2, Period2>> {
typedef chrono::duration<typename common_type<Rep1, Rep2>::type, see below> type;
};
1 The period of the duration indicated by this specialization of common_type shall be the greatest common divisor of Period1 and Period2. [Note: This can be computed by forming a ratio of the greatest common divisor of Period1::num and Period2::num and the least common multiple of Period1::den and Period2::den. —endnote]
2 [ Note: The typedef name type is a synonym for the duration with the largest tick period possible where both duration arguments will convert to it without requiring a division operation. The representation of this type is intended to be able to hold any value resulting from this conversion with no truncation error, although floating-point durations may have round-off errors. — end note ]
---
The "Period" is a std::ratio, a compile-time rational class.
Howard
On Jul 21, 2011, at 10:44 AM, Christopher Henrich wrote:
> GCD and LCM can be defined for rationals, if one takes "divisible" to mean "divisible with an integer quotient" and, similarly, "multiple" to mean "multiple by an integer." Then, for example, GCD(1/2, 1/3) = 1/6 and LCM(1/2, 1/3) = 1.
>
> On Jul 21, 2011, at 7:44 AM, Júlio Hoffimann wrote:
>
>> Hi Thomas,
>>
>> Thank you for the intention, but as far as i know, GCD and LCM makes no sense in the rational field. Any rational number is divisible by all rationals. For instance, given two rationals b, r in Q, r != 0, you can write:
>>
>> b = (b * r^-1) * r
>>
>> and then b is divisible by r.
>>
>> Regards,
>> Júlio.
>>
>> 2011/7/21 Thomas Taylor <thomas.taylor_at_[hidden]>
>> Dear boost users and maintainers,
>>
>> As I needed these functions myself I have added a gcd and an lcm for the
>> rational type.
>> Feel free to do with the code whatever you want, if suitable add it to
>> rational.hpp. Presumably this could be reworked as partial specialisation of
>> boost::math::gcd and boost::math::lcm respectively (if there is a trait like
>> is_rational).
>>
>> HTH,
>> Thomas
>>
>>
>>
>> _______________________________________________
>> Boost-users mailing list
>> Boost-users_at_[hidden]
>> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>>
>> _______________________________________________
>> Boost-users mailing list
>> Boost-users_at_[hidden]
>> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>
> Christopher Henrich
> chenrich_at_[hidden]
> mathinteract.com
>
>
>
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
```