1*8b851043SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*8b851043SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*8b851043SAndrew Rist * or more contributor license agreements. See the NOTICE file
5*8b851043SAndrew Rist * distributed with this work for additional information
6*8b851043SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*8b851043SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*8b851043SAndrew Rist * "License"); you may not use this file except in compliance
9*8b851043SAndrew Rist * with the License. You may obtain a copy of the License at
10*8b851043SAndrew Rist *
11*8b851043SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*8b851043SAndrew Rist *
13*8b851043SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*8b851043SAndrew Rist * software distributed under the License is distributed on an
15*8b851043SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*8b851043SAndrew Rist * KIND, either express or implied. See the License for the
17*8b851043SAndrew Rist * specific language governing permissions and limitations
18*8b851043SAndrew Rist * under the License.
19*8b851043SAndrew Rist *
20*8b851043SAndrew Rist *************************************************************/
21*8b851043SAndrew Rist
22*8b851043SAndrew Rist
23cdf0e10cSrcweir #ifndef _FRACT_HXX
24cdf0e10cSrcweir #define _FRACT_HXX
25cdf0e10cSrcweir
26cdf0e10cSrcweir #include "tools/toolsdllapi.h"
27cdf0e10cSrcweir #include <tools/solar.h>
28cdf0e10cSrcweir
29cdf0e10cSrcweir class SvStream;
30cdf0e10cSrcweir // ------------
31cdf0e10cSrcweir // - Fraction -
32cdf0e10cSrcweir // ------------
33cdf0e10cSrcweir
34cdf0e10cSrcweir class TOOLS_DLLPUBLIC Fraction
35cdf0e10cSrcweir {
36cdf0e10cSrcweir private:
37cdf0e10cSrcweir long nNumerator;
38cdf0e10cSrcweir long nDenominator;
39cdf0e10cSrcweir
40cdf0e10cSrcweir public:
Fraction()41cdf0e10cSrcweir Fraction() { nNumerator = 0; nDenominator = 1; }
42cdf0e10cSrcweir Fraction( const Fraction & rFrac );
43cdf0e10cSrcweir Fraction( long nNum, long nDen=1 );
44cdf0e10cSrcweir Fraction( long nN1, long nN2, long nD1, long nD2 );
45cdf0e10cSrcweir Fraction( double dVal );
46cdf0e10cSrcweir
47cdf0e10cSrcweir sal_Bool IsValid() const;
48cdf0e10cSrcweir
GetNumerator() const49cdf0e10cSrcweir long GetNumerator() const { return nNumerator; }
GetDenominator() const50cdf0e10cSrcweir long GetDenominator() const { return nDenominator; }
51cdf0e10cSrcweir
52cdf0e10cSrcweir operator long() const;
53cdf0e10cSrcweir operator double() const;
54cdf0e10cSrcweir
55cdf0e10cSrcweir Fraction& operator=( const Fraction& rfrFrac );
56cdf0e10cSrcweir
57cdf0e10cSrcweir Fraction& operator+=( const Fraction& rfrFrac );
58cdf0e10cSrcweir Fraction& operator-=( const Fraction& rfrFrac );
59cdf0e10cSrcweir Fraction& operator*=( const Fraction& rfrFrac );
60cdf0e10cSrcweir Fraction& operator/=( const Fraction& rfrFrac );
61cdf0e10cSrcweir
62cdf0e10cSrcweir void ReduceInaccurate( unsigned nSignificantBits );
63cdf0e10cSrcweir #ifdef __BORLANDC__
64cdf0e10cSrcweir friend Fraction operator+( const Fraction& rVal1, const Fraction& rVal2 );
65cdf0e10cSrcweir friend Fraction operator-( const Fraction& rVal1, const Fraction& rVal2 );
66cdf0e10cSrcweir friend Fraction operator*( const Fraction& rVal1, const Fraction& rVal2 );
67cdf0e10cSrcweir friend Fraction operator/( const Fraction& rVal1, const Fraction& rVal2 );
68cdf0e10cSrcweir
69cdf0e10cSrcweir friend sal_Bool operator==( const Fraction& rVal1, const Fraction& rVal2 );
70cdf0e10cSrcweir friend sal_Bool operator!=( const Fraction& rVal1, const Fraction& rVal2 );
71cdf0e10cSrcweir friend sal_Bool operator< ( const Fraction& rVal1, const Fraction& rVal2 );
72cdf0e10cSrcweir friend sal_Bool operator> ( const Fraction& rVal1, const Fraction& rVal2 );
73cdf0e10cSrcweir friend sal_Bool operator<=( const Fraction& rVal1, const Fraction& rVal2 );
74cdf0e10cSrcweir friend sal_Bool operator>=( const Fraction& rVal1, const Fraction& rVal2 );
75cdf0e10cSrcweir #else
76cdf0e10cSrcweir friend inline Fraction operator+( const Fraction& rVal1, const Fraction& rVal2 );
77cdf0e10cSrcweir friend inline Fraction operator-( const Fraction& rVal1, const Fraction& rVal2 );
78cdf0e10cSrcweir friend inline Fraction operator*( const Fraction& rVal1, const Fraction& rVal2 );
79cdf0e10cSrcweir friend inline Fraction operator/( const Fraction& rVal1, const Fraction& rVal2 );
80cdf0e10cSrcweir
81cdf0e10cSrcweir TOOLS_DLLPUBLIC friend sal_Bool operator==( const Fraction& rVal1, const Fraction& rVal2 );
82cdf0e10cSrcweir friend inline sal_Bool operator!=( const Fraction& rVal1, const Fraction& rVal2 );
83cdf0e10cSrcweir TOOLS_DLLPUBLIC friend sal_Bool operator< ( const Fraction& rVal1, const Fraction& rVal2 );
84cdf0e10cSrcweir TOOLS_DLLPUBLIC friend sal_Bool operator> ( const Fraction& rVal1, const Fraction& rVal2 );
85cdf0e10cSrcweir friend inline sal_Bool operator<=( const Fraction& rVal1, const Fraction& rVal2 );
86cdf0e10cSrcweir friend inline sal_Bool operator>=( const Fraction& rVal1, const Fraction& rVal2 );
87cdf0e10cSrcweir #endif
88cdf0e10cSrcweir TOOLS_DLLPUBLIC friend SvStream& operator>>( SvStream& rIStream, Fraction& rFract );
89cdf0e10cSrcweir TOOLS_DLLPUBLIC friend SvStream& operator<<( SvStream& rOStream, const Fraction& rFract );
90cdf0e10cSrcweir };
91cdf0e10cSrcweir
Fraction(const Fraction & rFrac)92cdf0e10cSrcweir inline Fraction::Fraction( const Fraction& rFrac )
93cdf0e10cSrcweir {
94cdf0e10cSrcweir nNumerator = rFrac.nNumerator;
95cdf0e10cSrcweir nDenominator = rFrac.nDenominator;
96cdf0e10cSrcweir }
97cdf0e10cSrcweir
operator =(const Fraction & rFrac)98cdf0e10cSrcweir inline Fraction& Fraction::operator=( const Fraction& rFrac )
99cdf0e10cSrcweir {
100cdf0e10cSrcweir nNumerator = rFrac.nNumerator;
101cdf0e10cSrcweir nDenominator = rFrac.nDenominator;
102cdf0e10cSrcweir return *this;
103cdf0e10cSrcweir }
104cdf0e10cSrcweir
IsValid() const105cdf0e10cSrcweir inline sal_Bool Fraction::IsValid() const
106cdf0e10cSrcweir {
107cdf0e10cSrcweir return (nDenominator > 0);
108cdf0e10cSrcweir }
109cdf0e10cSrcweir
110cdf0e10cSrcweir inline Fraction::operator long() const
111cdf0e10cSrcweir {
112cdf0e10cSrcweir if ( nDenominator > 0 )
113cdf0e10cSrcweir return (nNumerator / nDenominator);
114cdf0e10cSrcweir else
115cdf0e10cSrcweir return 0;
116cdf0e10cSrcweir }
117cdf0e10cSrcweir
operator +(const Fraction & rVal1,const Fraction & rVal2)118cdf0e10cSrcweir inline Fraction operator+( const Fraction& rVal1, const Fraction& rVal2 )
119cdf0e10cSrcweir {
120cdf0e10cSrcweir Fraction aErg( rVal1 );
121cdf0e10cSrcweir aErg += rVal2;
122cdf0e10cSrcweir return aErg;
123cdf0e10cSrcweir }
124cdf0e10cSrcweir
operator -(const Fraction & rVal1,const Fraction & rVal2)125cdf0e10cSrcweir inline Fraction operator-( const Fraction& rVal1, const Fraction& rVal2 )
126cdf0e10cSrcweir {
127cdf0e10cSrcweir Fraction aErg( rVal1 );
128cdf0e10cSrcweir aErg -= rVal2;
129cdf0e10cSrcweir return aErg;
130cdf0e10cSrcweir }
131cdf0e10cSrcweir
operator *(const Fraction & rVal1,const Fraction & rVal2)132cdf0e10cSrcweir inline Fraction operator*( const Fraction& rVal1, const Fraction& rVal2 )
133cdf0e10cSrcweir {
134cdf0e10cSrcweir Fraction aErg( rVal1 );
135cdf0e10cSrcweir aErg *= rVal2;
136cdf0e10cSrcweir return aErg;
137cdf0e10cSrcweir }
138cdf0e10cSrcweir
operator /(const Fraction & rVal1,const Fraction & rVal2)139cdf0e10cSrcweir inline Fraction operator/( const Fraction& rVal1, const Fraction& rVal2 )
140cdf0e10cSrcweir {
141cdf0e10cSrcweir Fraction aErg( rVal1 );
142cdf0e10cSrcweir aErg /= rVal2;
143cdf0e10cSrcweir return aErg;
144cdf0e10cSrcweir }
145cdf0e10cSrcweir
operator !=(const Fraction & rVal1,const Fraction & rVal2)146cdf0e10cSrcweir inline sal_Bool operator !=( const Fraction& rVal1, const Fraction& rVal2 )
147cdf0e10cSrcweir {
148cdf0e10cSrcweir return !(rVal1 == rVal2);
149cdf0e10cSrcweir }
150cdf0e10cSrcweir
operator <=(const Fraction & rVal1,const Fraction & rVal2)151cdf0e10cSrcweir inline sal_Bool operator <=( const Fraction& rVal1, const Fraction& rVal2 )
152cdf0e10cSrcweir {
153cdf0e10cSrcweir return !(rVal1 > rVal2);
154cdf0e10cSrcweir }
155cdf0e10cSrcweir
operator >=(const Fraction & rVal1,const Fraction & rVal2)156cdf0e10cSrcweir inline sal_Bool operator >=( const Fraction& rVal1, const Fraction& rVal2 )
157cdf0e10cSrcweir {
158cdf0e10cSrcweir return !(rVal1 < rVal2);
159cdf0e10cSrcweir }
160cdf0e10cSrcweir
161cdf0e10cSrcweir #endif // _FRACT_HXX
162