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