diff options
author | paolo <paolo@138bc75d-0d04-0410-961f-82ee72b054a4> | 2011-03-02 14:57:57 +0000 |
---|---|---|
committer | paolo <paolo@138bc75d-0d04-0410-961f-82ee72b054a4> | 2011-03-02 14:57:57 +0000 |
commit | 62fb9a592c0ce98fcd55ce0e687980c10373889b (patch) | |
tree | a31aaed4cbda3c591759e1f60f825767aebffa4d /libstdc++-v3/include | |
parent | 46313bebd121a41af678ad5d7f15cdb9b9986e14 (diff) | |
download | gcc-62fb9a592c0ce98fcd55ce0e687980c10373889b.tar.gz |
2011-03-02 Marc Glisse <marc.glisse@normalesup.org>
PR libstdc++/47913
* include/std/ratio (ratio_add): Avoid denominator overflow.
* testsuite/20_util/ratio/operations/47913.cc: New.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@170616 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'libstdc++-v3/include')
-rw-r--r-- | libstdc++-v3/include/std/ratio | 16 |
1 files changed, 10 insertions, 6 deletions
diff --git a/libstdc++-v3/include/std/ratio b/libstdc++-v3/include/std/ratio index 2b10da7c688..30bce162e16 100644 --- a/libstdc++-v3/include/std/ratio +++ b/libstdc++-v3/include/std/ratio @@ -177,15 +177,19 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION struct ratio_add { private: - static const intmax_t __gcd = + static constexpr intmax_t __gcd = __static_gcd<_R1::den, _R2::den>::value; + static constexpr intmax_t __n = __safe_add< + __safe_multiply<_R1::num, (_R2::den / __gcd)>::value, + __safe_multiply<_R2::num, (_R1::den / __gcd)>::value>::value; + + // The new numerator may have common factors with the denominator, + // but they have to also be factors of __gcd. + static constexpr intmax_t __gcd2 = __static_gcd<__n, __gcd>::value; public: - typedef ratio< - __safe_add< - __safe_multiply<_R1::num, (_R2::den / __gcd)>::value, - __safe_multiply<_R2::num, (_R1::den / __gcd)>::value>::value, - __safe_multiply<_R1::den, (_R2::den / __gcd)>::value> type; + typedef ratio<__n / __gcd2, + __safe_multiply<_R1::den / __gcd2, _R2::den / __gcd>::value> type; static constexpr intmax_t num = type::num; static constexpr intmax_t den = type::den; |