xref: /aoo41x/main/tools/inc/tools/fract.hxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 #ifndef _FRACT_HXX
28 #define _FRACT_HXX
29 
30 #include "tools/toolsdllapi.h"
31 #include <tools/solar.h>
32 
33 class SvStream;
34 // ------------
35 // - Fraction -
36 // ------------
37 
38 class TOOLS_DLLPUBLIC Fraction
39 {
40 private:
41     long            nNumerator;
42     long            nDenominator;
43 
44 public:
45                     Fraction() { nNumerator = 0; nDenominator = 1; }
46                     Fraction( const Fraction & rFrac );
47                     Fraction( long nNum, long nDen=1 );
48                     Fraction( long nN1, long nN2, long nD1, long nD2 );
49                     Fraction( double dVal );
50 
51     sal_Bool            IsValid() const;
52 
53     long            GetNumerator() const { return nNumerator; }
54     long            GetDenominator() const { return nDenominator; }
55 
56     operator        long() const;
57     operator        double() const;
58 
59     Fraction&       operator=( const Fraction& rfrFrac );
60 
61     Fraction&       operator+=( const Fraction& rfrFrac );
62     Fraction&       operator-=( const Fraction& rfrFrac );
63     Fraction&       operator*=( const Fraction& rfrFrac );
64     Fraction&       operator/=( const Fraction& rfrFrac );
65 
66     void            ReduceInaccurate( unsigned nSignificantBits );
67 #ifdef __BORLANDC__
68     friend          Fraction operator+( const Fraction& rVal1, const Fraction& rVal2 );
69     friend          Fraction operator-( const Fraction& rVal1, const Fraction& rVal2 );
70     friend          Fraction operator*( const Fraction& rVal1, const Fraction& rVal2 );
71     friend          Fraction operator/( const Fraction& rVal1, const Fraction& rVal2 );
72 
73     friend          sal_Bool operator==( const Fraction& rVal1, const Fraction& rVal2 );
74     friend          sal_Bool operator!=( const Fraction& rVal1, const Fraction& rVal2 );
75     friend          sal_Bool operator< ( const Fraction& rVal1, const Fraction& rVal2 );
76     friend          sal_Bool operator> ( const Fraction& rVal1, const Fraction& rVal2 );
77     friend          sal_Bool operator<=( const Fraction& rVal1, const Fraction& rVal2 );
78     friend          sal_Bool operator>=( const Fraction& rVal1, const Fraction& rVal2 );
79 #else
80     friend inline   Fraction operator+( const Fraction& rVal1, const Fraction& rVal2 );
81     friend inline   Fraction operator-( const Fraction& rVal1, const Fraction& rVal2 );
82     friend inline   Fraction operator*( const Fraction& rVal1, const Fraction& rVal2 );
83     friend inline   Fraction operator/( const Fraction& rVal1, const Fraction& rVal2 );
84 
85     TOOLS_DLLPUBLIC friend          sal_Bool operator==( const Fraction& rVal1, const Fraction& rVal2 );
86     friend inline   sal_Bool operator!=( const Fraction& rVal1, const Fraction& rVal2 );
87     TOOLS_DLLPUBLIC friend          sal_Bool operator< ( const Fraction& rVal1, const Fraction& rVal2 );
88     TOOLS_DLLPUBLIC friend          sal_Bool operator> ( const Fraction& rVal1, const Fraction& rVal2 );
89     friend inline   sal_Bool operator<=( const Fraction& rVal1, const Fraction& rVal2 );
90     friend inline   sal_Bool operator>=( const Fraction& rVal1, const Fraction& rVal2 );
91 #endif
92     TOOLS_DLLPUBLIC friend SvStream& operator>>( SvStream& rIStream, Fraction& rFract );
93     TOOLS_DLLPUBLIC friend SvStream& operator<<( SvStream& rOStream, const Fraction& rFract );
94 };
95 
96 inline Fraction::Fraction( const Fraction& rFrac )
97 {
98     nNumerator   = rFrac.nNumerator;
99     nDenominator = rFrac.nDenominator;
100 }
101 
102 inline Fraction& Fraction::operator=( const Fraction& rFrac )
103 {
104     nNumerator   = rFrac.nNumerator;
105     nDenominator = rFrac.nDenominator;
106     return *this;
107 }
108 
109 inline sal_Bool Fraction::IsValid() const
110 {
111     return (nDenominator > 0);
112 }
113 
114 inline Fraction::operator long() const
115 {
116     if ( nDenominator > 0 )
117         return (nNumerator / nDenominator);
118     else
119         return 0;
120 }
121 
122 inline Fraction operator+( const Fraction& rVal1, const Fraction& rVal2 )
123 {
124     Fraction aErg( rVal1 );
125     aErg += rVal2;
126     return aErg;
127 }
128 
129 inline Fraction operator-( const Fraction& rVal1, const Fraction& rVal2 )
130 {
131     Fraction aErg( rVal1 );
132     aErg -= rVal2;
133     return aErg;
134 }
135 
136 inline Fraction operator*( const Fraction& rVal1, const Fraction& rVal2 )
137 {
138     Fraction aErg( rVal1 );
139     aErg *= rVal2;
140     return aErg;
141 }
142 
143 inline Fraction operator/( const Fraction& rVal1, const Fraction& rVal2 )
144 {
145     Fraction aErg( rVal1 );
146     aErg /= rVal2;
147     return aErg;
148 }
149 
150 inline sal_Bool operator !=( const Fraction& rVal1, const Fraction& rVal2 )
151 {
152     return !(rVal1 == rVal2);
153 }
154 
155 inline sal_Bool operator <=( const Fraction& rVal1, const Fraction& rVal2 )
156 {
157     return !(rVal1 > rVal2);
158 }
159 
160 inline sal_Bool operator >=( const Fraction& rVal1, const Fraction& rVal2 )
161 {
162     return !(rVal1 < rVal2);
163 }
164 
165 #endif // _FRACT_HXX
166