1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
22 #include "tools/toolsdllapi.h"
23 #include <tools/solar.h>
27 class TOOLS_DLLPUBLIC SAL_WARN_UNUSED Fraction
34 Fraction() { nNumerator
= 0; nDenominator
= 1; }
35 Fraction( const Fraction
& rFrac
);
36 Fraction( long nNum
, long nDen
=1 );
37 Fraction( double dVal
);
41 long GetNumerator() const { return nNumerator
; }
42 long GetDenominator() const { return nDenominator
; }
44 operator long() const;
45 operator double() const;
47 Fraction
& operator=( const Fraction
& rfrFrac
);
49 Fraction
& operator+=( const Fraction
& rfrFrac
);
50 Fraction
& operator-=( const Fraction
& rfrFrac
);
51 Fraction
& operator*=( const Fraction
& rfrFrac
);
52 Fraction
& operator/=( const Fraction
& rfrFrac
);
54 void ReduceInaccurate( unsigned nSignificantBits
);
56 friend inline Fraction
operator+( const Fraction
& rVal1
, const Fraction
& rVal2
);
57 friend inline Fraction
operator-( const Fraction
& rVal1
, const Fraction
& rVal2
);
58 friend inline Fraction
operator*( const Fraction
& rVal1
, const Fraction
& rVal2
);
59 friend inline Fraction
operator/( const Fraction
& rVal1
, const Fraction
& rVal2
);
61 TOOLS_DLLPUBLIC
friend bool operator==( const Fraction
& rVal1
, const Fraction
& rVal2
);
62 friend inline bool operator!=( const Fraction
& rVal1
, const Fraction
& rVal2
);
63 TOOLS_DLLPUBLIC
friend bool operator< ( const Fraction
& rVal1
, const Fraction
& rVal2
);
64 TOOLS_DLLPUBLIC
friend bool operator> ( const Fraction
& rVal1
, const Fraction
& rVal2
);
65 friend inline bool operator<=( const Fraction
& rVal1
, const Fraction
& rVal2
);
66 friend inline bool operator>=( const Fraction
& rVal1
, const Fraction
& rVal2
);
68 TOOLS_DLLPUBLIC
friend SvStream
& operator>>( SvStream
& rIStream
, Fraction
& rFract
);
69 TOOLS_DLLPUBLIC
friend SvStream
& operator<<( SvStream
& rOStream
, const Fraction
& rFract
);
72 inline Fraction::Fraction( const Fraction
& rFrac
)
74 nNumerator
= rFrac
.nNumerator
;
75 nDenominator
= rFrac
.nDenominator
;
78 inline Fraction
& Fraction::operator=( const Fraction
& rFrac
)
80 nNumerator
= rFrac
.nNumerator
;
81 nDenominator
= rFrac
.nDenominator
;
85 inline bool Fraction::IsValid() const
87 return (nDenominator
> 0);
90 inline Fraction::operator long() const
92 if ( nDenominator
> 0 )
93 return (nNumerator
/ nDenominator
);
98 inline Fraction
operator+( const Fraction
& rVal1
, const Fraction
& rVal2
)
100 Fraction
aErg( rVal1
);
105 inline Fraction
operator-( const Fraction
& rVal1
, const Fraction
& rVal2
)
107 Fraction
aErg( rVal1
);
112 inline Fraction
operator*( const Fraction
& rVal1
, const Fraction
& rVal2
)
114 Fraction
aErg( rVal1
);
119 inline Fraction
operator/( const Fraction
& rVal1
, const Fraction
& rVal2
)
121 Fraction
aErg( rVal1
);
126 inline bool operator !=( const Fraction
& rVal1
, const Fraction
& rVal2
)
128 return !(rVal1
== rVal2
);
131 inline bool operator <=( const Fraction
& rVal1
, const Fraction
& rVal2
)
133 return !(rVal1
> rVal2
);
136 inline bool operator >=( const Fraction
& rVal1
, const Fraction
& rVal2
)
138 return !(rVal1
< rVal2
);
143 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */