[Libreoffice-commits] core.git: tools/source
Caolán McNamara
caolanm at redhat.com
Tue Mar 21 11:46:22 UTC 2017
tools/source/generic/fract.cxx | 22 ++++++++++++++++++++--
1 file changed, 20 insertions(+), 2 deletions(-)
New commits:
commit 3ff957b0a45a1c7a2666103a6a6783e69de3446c
Author: Caolán McNamara <caolanm at redhat.com>
Date: Tue Mar 21 10:35:55 2017 +0000
duplicate rational::operator*= into tools
for modification, no logic changed intended in this step
Change-Id: Ib41051a83bc9e37677d765e51e9f56cede0efb3e
diff --git a/tools/source/generic/fract.cxx b/tools/source/generic/fract.cxx
index c79b24bbf3a3..068a2b6429a8 100644
--- a/tools/source/generic/fract.cxx
+++ b/tools/source/generic/fract.cxx
@@ -170,6 +170,24 @@ Fraction& Fraction::operator -= ( const Fraction& rVal )
return *this;
}
+namespace
+{
+ template<typename T> void multiply_by(boost::rational<T>& i, const boost::rational<T>& r)
+ {
+ // Protect against self-modification
+ T num = r.numerator();
+ T den = r.denominator();
+
+ // Avoid overflow and preserve normalization
+ T gcd1 = boost::integer::gcd(i.numerator(), den);
+ T gcd2 = boost::integer::gcd(num, i.denominator());
+ num = (i.numerator() / gcd1) * (num / gcd2);
+ den = (i.denominator() / gcd2) * (den / gcd1);
+
+ i.assign(num, den);
+ }
+}
+
Fraction& Fraction::operator *= ( const Fraction& rVal )
{
if ( !rVal.mpImpl->valid )
@@ -181,9 +199,9 @@ Fraction& Fraction::operator *= ( const Fraction& rVal )
return *this;
}
- mpImpl->value *= rVal.mpImpl->value;
+ multiply_by(mpImpl->value, rVal.mpImpl->value);
- if ( HasOverflowValue() )
+ if (HasOverflowValue())
{
mpImpl->valid = false;
}
More information about the Libreoffice-commits
mailing list