Kaydet (Commit) 2ce0aede authored tarafından Juan Picca's avatar Juan Picca Kaydeden (comit) David Tardon

fdo#81356: use boost::rational internally in Fraction

Change-Id: I6f40eafee7652209395bd471e3508fe3a3d19d73
Reviewed-on: https://gerrit.libreoffice.org/12085Reviewed-by: 's avatarDavid Tardon <dtardon@redhat.com>
Tested-by: 's avatarDavid Tardon <dtardon@redhat.com>
üst 8c5f6403
......@@ -19,26 +19,32 @@
#ifndef INCLUDED_TOOLS_FRACT_HXX
#define INCLUDED_TOOLS_FRACT_HXX
#include <boost/rational.hpp>
#include <sal/log.hxx>
#include <tools/toolsdllapi.h>
class SvStream;
// This class uses the platform defined type 'long' as valid values but do all
// calculations using sal_Int64 with checks for 'long' overflows.
class TOOLS_DLLPUBLIC SAL_WARN_UNUSED Fraction
{
private:
long nNumerator;
long nDenominator;
bool valid;
boost::rational<sal_Int64> value;
bool HasOverflowValue();
public:
Fraction() { nNumerator = 0; nDenominator = 1; }
Fraction() { valid = true; }
Fraction( const Fraction & rFrac );
Fraction( long nNum, long nDen=1 );
Fraction( double dVal );
bool IsValid() const;
long GetNumerator() const { return nNumerator; }
long GetDenominator() const { return nDenominator; }
long GetNumerator() const;
long GetDenominator() const;
operator long() const;
operator double() const;
......@@ -70,28 +76,50 @@ public:
inline Fraction::Fraction( const Fraction& rFrac )
{
nNumerator = rFrac.nNumerator;
nDenominator = rFrac.nDenominator;
valid = rFrac.valid;
if ( valid )
value.assign( rFrac.value.numerator(), rFrac.value.denominator() );
}
inline long Fraction::GetNumerator() const
{
if ( !valid ) {
SAL_WARN( "tools.fraction", "'GetNumerator()' on invalid fraction" );
return 0;
}
return value.numerator();
}
inline long Fraction::GetDenominator() const {
if ( !valid ) {
SAL_WARN( "tools.fraction", "'GetDenominator()' on invalid fraction" );
return -1;
}
return value.denominator();
}
inline Fraction& Fraction::operator=( const Fraction& rFrac )
{
nNumerator = rFrac.nNumerator;
nDenominator = rFrac.nDenominator;
if ( this != &rFrac ) {
valid = rFrac.valid;
if ( valid )
value.assign( rFrac.value.numerator(), rFrac.value.denominator() );
}
return *this;
}
inline bool Fraction::IsValid() const
{
return (nDenominator > 0);
return valid;
}
inline Fraction::operator long() const
{
if ( nDenominator > 0 )
return (nNumerator / nDenominator);
else
if ( !valid ) {
SAL_WARN( "tools.fraction", "'operator long()' on invalid fraction" );
return 0;
}
return boost::rational_cast<long>(value);
}
inline Fraction operator+( const Fraction& rVal1, const Fraction& rVal2 )
......
......@@ -96,9 +96,16 @@ public:
CPPUNIT_ASSERT_EQUAL(1L, f.GetDenominator());
}
void testCreateFromDoubleIn32BitsPlatform() {
// This pass in 64 bits but fail in 32 bits
Fraction f(0.960945);
CPPUNIT_ASSERT_EQUAL(true, f.IsValid());
}
CPPUNIT_TEST_SUITE(FractionTest);
CPPUNIT_TEST(testFraction);
CPPUNIT_TEST(testMinLongDouble);
CPPUNIT_TEST(testCreateFromDoubleIn32BitsPlatform);
CPPUNIT_TEST_SUITE_END();
};
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment