1 // This may look like C code, but it is really -*- C++ -*-
4 Copyright (C) 1988 Free Software Foundation
5 written by Doug Lea (dl@rocky.oswego.edu)
7 This file is part of GNU CC.
9 GNU CC is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY. No author or distributor
11 accepts responsibility to anyone for the consequences of using it
12 or for whether it serves any particular purpose or works at all,
13 unless he says so in writing. Refer to the GNU CC General Public
14 License for full details.
16 Everyone is granted permission to copy, modify and redistribute
17 GNU CC, but only under the conditions described in the
18 GNU CC General Public License. A copy of this license is
19 supposed to have been given to you along with GNU CC so you
20 can know your rights and responsibilities. It should be in a
21 file named COPYING. Among other things, the copyright notice
22 and this notice must be preserved on all copies.
46 Rational(long n
, long d
= 1);
47 Rational(const Integer
& n
);
48 Rational(const Integer
& n
, const Integer
& d
);
49 Rational(const Rational
&);
53 void operator = (const Rational
& y
);
55 friend int operator == (const Rational
& x
, const Rational
& y
);
56 friend int operator != (const Rational
& x
, const Rational
& y
);
57 friend int operator < (const Rational
& x
, const Rational
& y
);
58 friend int operator <= (const Rational
& x
, const Rational
& y
);
59 friend int operator > (const Rational
& x
, const Rational
& y
);
60 friend int operator >= (const Rational
& x
, const Rational
& y
);
62 friend Rational
operator + (const Rational
& x
, const Rational
& y
);
63 friend Rational
operator - (const Rational
& x
, const Rational
& y
);
64 friend Rational
operator * (const Rational
& x
, const Rational
& y
);
65 friend Rational
operator / (const Rational
& x
, const Rational
& y
);
67 void operator += (const Rational
& y
);
68 void operator -= (const Rational
& y
);
69 void operator *= (const Rational
& y
);
70 void operator /= (const Rational
& y
);
73 friend Rational
operator <? (const Rational
& x
, const Rational
& y
); // min
74 friend Rational
operator >? (const Rational
& x
, const Rational
& y
); // max
77 friend Rational
operator - (const Rational
& x
);
80 // builtin Rational functions
83 void negate(); // x = -x
84 void invert(); // x = 1/x
86 friend int sign(const Rational
& x
); // -1, 0, or +1
87 friend Rational
abs(const Rational
& x
); // absolute value
88 friend Rational
sqr(const Rational
& x
); // square
89 friend Rational
pow(const Rational
& x
, long y
);
90 friend Rational
pow(const Rational
& x
, Integer
& y
);
91 const Integer
& numerator() const;
92 const Integer
& denominator() const;
94 // coercion & conversion
96 operator double() const;
97 friend Integer
floor(const Rational
& x
);
98 friend Integer
ceil(const Rational
& x
);
99 friend Integer
trunc(const Rational
& x
);
100 friend Integer
round(const Rational
& x
);
102 friend istream
& operator >> (istream
& s
, Rational
& y
);
103 friend ostream
& operator << (ostream
& s
, const Rational
& y
);
106 // procedural versions of operators
108 friend int compare(const Rational
& x
, const Rational
& y
);
109 friend void add(const Rational
& x
, const Rational
& y
, Rational
& dest
);
110 friend void sub(const Rational
& x
, const Rational
& y
, Rational
& dest
);
111 friend void mul(const Rational
& x
, const Rational
& y
, Rational
& dest
);
112 friend void div(const Rational
& x
, const Rational
& y
, Rational
& dest
);
116 volatile void error(const char* msg
) const;
121 typedef Rational RatTmp
; // backwards compatibility
123 #if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
125 inline Rational::Rational() {}
126 inline Rational::~Rational() {}
128 inline Rational::Rational(const Rational
& y
) :num(y
.num
), den(y
.den
) {}
130 inline Rational::Rational(const Integer
& n
) :num(n
), den(1) {}
132 inline Rational::Rational(const Integer
& n
, const Integer
& d
) :num(n
),den(d
)
137 inline Rational::Rational(long n
, long d
) :num(n
), den(d
)
142 inline void Rational::operator = (const Rational
& y
)
144 num
= y
.num
; den
= y
.den
;
147 inline int operator == (const Rational
& x
, const Rational
& y
)
149 return compare(x
.num
, y
.num
) == 0 && compare(x
.den
, y
.den
) == 0;
152 inline int operator != (const Rational
& x
, const Rational
& y
)
154 return compare(x
.num
, y
.num
) != 0 || compare(x
.den
, y
.den
) != 0;
157 inline int operator < (const Rational
& x
, const Rational
& y
)
159 return compare(x
, y
) < 0;
162 inline int operator <= (const Rational
& x
, const Rational
& y
)
164 return compare(x
, y
) <= 0;
167 inline int operator > (const Rational
& x
, const Rational
& y
)
169 return compare(x
, y
) > 0;
172 inline int operator >= (const Rational
& x
, const Rational
& y
)
174 return compare(x
, y
) >= 0;
177 inline int sign(const Rational
& x
)
182 inline void Rational::negate()
188 inline void Rational::operator += (const Rational
& y
)
190 add(*this, y
, *this);
193 inline void Rational::operator -= (const Rational
& y
)
195 sub(*this, y
, *this);
198 inline void Rational::operator *= (const Rational
& y
)
200 mul(*this, y
, *this);
203 inline void Rational::operator /= (const Rational
& y
)
205 div(*this, y
, *this);
208 inline const Integer
& Rational::numerator() const { return num
; }
209 inline const Integer
& Rational::denominator() const { return den
; }
210 inline Rational::operator double() const { return ratio(num
, den
); }
213 inline Rational
operator <? (const Rational
& x
, const Rational
& y
)
215 if (compare(x
, y
) <= 0) return x
; else return y
;
218 inline Rational
operator >? (const Rational
& x
, const Rational
& y
)
220 if (compare(x
, y
) >= 0) return x
; else return y
;
224 #if defined(__GNUG__) && !defined(NO_NRV)
226 inline Rational
operator + (const Rational
& x
, const Rational
& y
) return r
231 inline Rational
operator - (const Rational
& x
, const Rational
& y
) return r
236 inline Rational
operator * (const Rational
& x
, const Rational
& y
) return r
241 inline Rational
operator / (const Rational
& x
, const Rational
& y
) return r
248 inline Rational
operator + (const Rational
& x
, const Rational
& y
)
250 Rational r
; add(x
, y
, r
); return r
;
253 inline Rational
operator - (const Rational
& x
, const Rational
& y
)
255 Rational r
; sub(x
, y
, r
); return r
;
258 inline Rational
operator * (const Rational
& x
, const Rational
& y
)
260 Rational r
; mul(x
, y
, r
); return r
;
263 inline Rational
operator / (const Rational
& x
, const Rational
& y
)
265 Rational r
; div(x
, y
, r
); return r
;