Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
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/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
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 .
18 : : */
19 : : #ifndef _FRACT_HXX
20 : : #define _FRACT_HXX
21 : :
22 : : #include "tools/toolsdllapi.h"
23 : : #include <tools/solar.h>
24 : :
25 : : class SvStream;
26 : :
27 : : class TOOLS_DLLPUBLIC Fraction
28 : : {
29 : : private:
30 : : long nNumerator;
31 : : long nDenominator;
32 : :
33 : : public:
34 : 185780 : Fraction() { nNumerator = 0; nDenominator = 1; }
35 : : Fraction( const Fraction & rFrac );
36 : : Fraction( long nNum, long nDen=1 );
37 : : Fraction( double dVal );
38 : :
39 : : bool IsValid() const;
40 : :
41 : 4449500 : long GetNumerator() const { return nNumerator; }
42 : 6312482 : long GetDenominator() const { return nDenominator; }
43 : :
44 : : operator long() const;
45 : : operator double() const;
46 : :
47 : : Fraction& operator=( const Fraction& rfrFrac );
48 : :
49 : : Fraction& operator+=( const Fraction& rfrFrac );
50 : : Fraction& operator-=( const Fraction& rfrFrac );
51 : : Fraction& operator*=( const Fraction& rfrFrac );
52 : : Fraction& operator/=( const Fraction& rfrFrac );
53 : :
54 : : void ReduceInaccurate( unsigned nSignificantBits );
55 : :
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 );
60 : :
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 );
67 : :
68 : : TOOLS_DLLPUBLIC friend SvStream& operator>>( SvStream& rIStream, Fraction& rFract );
69 : : TOOLS_DLLPUBLIC friend SvStream& operator<<( SvStream& rOStream, const Fraction& rFract );
70 : : };
71 : :
72 : 3536511 : inline Fraction::Fraction( const Fraction& rFrac )
73 : : {
74 : 3536511 : nNumerator = rFrac.nNumerator;
75 : 3536511 : nDenominator = rFrac.nDenominator;
76 : 3536511 : }
77 : :
78 : 465241 : inline Fraction& Fraction::operator=( const Fraction& rFrac )
79 : : {
80 : 465241 : nNumerator = rFrac.nNumerator;
81 : 465241 : nDenominator = rFrac.nDenominator;
82 : 465241 : return *this;
83 : : }
84 : :
85 : 1556 : inline bool Fraction::IsValid() const
86 : : {
87 : 1556 : return (nDenominator > 0);
88 : : }
89 : :
90 : 35523 : inline Fraction::operator long() const
91 : : {
92 [ + - ]: 35523 : if ( nDenominator > 0 )
93 : 35523 : return (nNumerator / nDenominator);
94 : : else
95 : 35523 : return 0;
96 : : }
97 : :
98 : 0 : inline Fraction operator+( const Fraction& rVal1, const Fraction& rVal2 )
99 : : {
100 : 0 : Fraction aErg( rVal1 );
101 : 0 : aErg += rVal2;
102 : 0 : return aErg;
103 : : }
104 : :
105 : : inline Fraction operator-( const Fraction& rVal1, const Fraction& rVal2 )
106 : : {
107 : : Fraction aErg( rVal1 );
108 : : aErg -= rVal2;
109 : : return aErg;
110 : : }
111 : :
112 : 52 : inline Fraction operator*( const Fraction& rVal1, const Fraction& rVal2 )
113 : : {
114 : 52 : Fraction aErg( rVal1 );
115 : 52 : aErg *= rVal2;
116 : 52 : return aErg;
117 : : }
118 : :
119 : 2436 : inline Fraction operator/( const Fraction& rVal1, const Fraction& rVal2 )
120 : : {
121 : 2436 : Fraction aErg( rVal1 );
122 : 2436 : aErg /= rVal2;
123 : 2436 : return aErg;
124 : : }
125 : :
126 : 41598 : inline bool operator !=( const Fraction& rVal1, const Fraction& rVal2 )
127 : : {
128 : 41598 : return !(rVal1 == rVal2);
129 : : }
130 : :
131 : 0 : inline bool operator <=( const Fraction& rVal1, const Fraction& rVal2 )
132 : : {
133 : 0 : return !(rVal1 > rVal2);
134 : : }
135 : :
136 : : inline bool operator >=( const Fraction& rVal1, const Fraction& rVal2 )
137 : : {
138 : : return !(rVal1 < rVal2);
139 : : }
140 : :
141 : : #endif
142 : :
143 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|