summaryrefslogtreecommitdiff
path: root/gnu/lib/libg++/g++-include/Rational.h
blob: 3e7e35f1b735e73cee967958430fb287cafde881 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// This may look like C code, but it is really -*- C++ -*-

/* 
Copyright (C) 1988 Free Software Foundation
    written by Doug Lea (dl@rocky.oswego.edu)

This file is part of GNU CC.

GNU CC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.  No author or distributor
accepts responsibility to anyone for the consequences of using it
or for whether it serves any particular purpose or works at all,
unless he says so in writing.  Refer to the GNU CC General Public
License for full details.

Everyone is granted permission to copy, modify and redistribute
GNU CC, but only under the conditions described in the
GNU CC General Public License.   A copy of this license is
supposed to have been given to you along with GNU CC so you
can know your rights and responsibilities.  It should be in a
file named COPYING.  Among other things, the copyright notice
and this notice must be preserved on all copies.  
*/

#ifndef _Rational_h
#ifdef __GNUG__
#pragma once
#pragma interface
#endif
#define _Rational_h 1

#include <Integer.h>
#include <math.h>

class Rational
{
protected:
  Integer          num;
  Integer          den;

  void             normalize();

public:
                   Rational();
                   Rational(double);
                   Rational(long n, long d = 1);
                   Rational(const Integer& n);
                   Rational(const Integer& n, const Integer& d);
                   Rational(const Rational&);

                  ~Rational();

  void             operator =  (const Rational& y);

  friend int       operator == (const Rational& x, const Rational& y);
  friend int       operator != (const Rational& x, const Rational& y);
  friend int       operator <  (const Rational& x, const Rational& y);
  friend int       operator <= (const Rational& x, const Rational& y);
  friend int       operator >  (const Rational& x, const Rational& y);
  friend int       operator >= (const Rational& x, const Rational& y);

  friend Rational  operator +  (const Rational& x, const Rational& y);
  friend Rational  operator -  (const Rational& x, const Rational& y);
  friend Rational  operator *  (const Rational& x, const Rational& y);
  friend Rational  operator /  (const Rational& x, const Rational& y);

  void             operator += (const Rational& y);
  void             operator -= (const Rational& y);
  void             operator *= (const Rational& y);
  void             operator /= (const Rational& y);

#ifdef __GNUG__
  friend Rational  operator <? (const Rational& x, const Rational& y); // min
  friend Rational  operator >? (const Rational& x, const Rational& y); // max
#endif

  friend Rational  operator - (const Rational& x);


// builtin Rational functions


  void             negate();                      // x = -x
  void             invert();                      // x = 1/x

  friend int       sign(const Rational& x);             // -1, 0, or +1
  friend Rational  abs(const Rational& x);              // absolute value
  friend Rational  sqr(const Rational& x);              // square
  friend Rational  pow(const Rational& x, long y);
  friend Rational  pow(const Rational& x, Integer& y);
  const Integer&   numerator() const;
  const Integer&   denominator() const;

// coercion & conversion

                   operator double() const;
  friend Integer   floor(const Rational& x);
  friend Integer   ceil(const Rational& x);
  friend Integer   trunc(const Rational& x);
  friend Integer   round(const Rational& x);

  friend istream&  operator >> (istream& s, Rational& y);
  friend ostream&  operator << (ostream& s, const Rational& y);


// procedural versions of operators

  friend int       compare(const Rational& x, const Rational& y);
  friend void      add(const Rational& x, const Rational& y, Rational& dest);
  friend void      sub(const Rational& x, const Rational& y, Rational& dest);
  friend void      mul(const Rational& x, const Rational& y, Rational& dest);
  friend void      div(const Rational& x, const Rational& y, Rational& dest);

// error detection

  volatile void    error(const char* msg) const;
  int              OK() const;

};

typedef Rational RatTmp; // backwards compatibility

#if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)

inline Rational::Rational()  {}
inline Rational::~Rational() {}

inline Rational::Rational(const Rational& y) :num(y.num), den(y.den) {}

inline Rational::Rational(const Integer& n) :num(n), den(1) {}

inline Rational::Rational(const Integer& n, const Integer& d) :num(n),den(d)
{
  normalize();
}

inline Rational::Rational(long n, long d) :num(n), den(d)
{
  normalize();
}

inline  void Rational::operator =  (const Rational& y)
{
  num = y.num;  den = y.den;
}

inline int operator == (const Rational& x, const Rational& y)
{
  return compare(x.num, y.num) == 0 && compare(x.den, y.den) == 0;
}

inline int operator != (const Rational& x, const Rational& y)
{
  return compare(x.num, y.num) != 0 || compare(x.den, y.den) != 0;
}

inline int operator <  (const Rational& x, const Rational& y)
{
  return compare(x, y) <  0; 
}

inline int operator <= (const Rational& x, const Rational& y)
{
  return compare(x, y) <= 0; 
}

inline int operator >  (const Rational& x, const Rational& y)
{
  return compare(x, y) >  0; 
}

inline int operator >= (const Rational& x, const Rational& y)
{
  return compare(x, y) >= 0; 
}

inline int sign(const Rational& x)
{
  return sign(x.num);
}

inline void Rational::negate()
{
  num.negate();
}


inline void Rational::operator += (const Rational& y) 
{
  add(*this, y, *this);
}

inline void Rational::operator -= (const Rational& y) 
{
  sub(*this, y, *this);
}

inline void Rational::operator *= (const Rational& y) 
{
  mul(*this, y, *this);
}

inline void Rational::operator /= (const Rational& y) 
{
  div(*this, y, *this);
}

inline const Integer& Rational::numerator() const { return num; }
inline const Integer& Rational::denominator() const { return den; }
inline Rational::operator double() const { return ratio(num, den); }

#ifdef __GNUG__
inline Rational operator <? (const Rational& x, const Rational& y)
{
  if (compare(x, y) <= 0) return x; else return y;
}

inline Rational operator >? (const Rational& x, const Rational& y)
{
  if (compare(x, y) >= 0) return x; else return y;
}
#endif

#if defined(__GNUG__) && !defined(NO_NRV)

inline Rational operator + (const Rational& x, const Rational& y) return r
{
  add(x, y, r);
}

inline Rational operator - (const Rational& x, const Rational& y) return r
{
  sub(x, y, r);
}

inline Rational operator * (const Rational& x, const Rational& y) return r
{
  mul(x, y, r);
}

inline Rational operator / (const Rational& x, const Rational& y) return r
{
  div(x, y, r);
}

#else /* NO_NRV */

inline Rational operator + (const Rational& x, const Rational& y) 
{
  Rational r; add(x, y, r); return r;
}

inline Rational operator - (const Rational& x, const Rational& y)
{
  Rational r; sub(x, y, r); return r;
}

inline Rational operator * (const Rational& x, const Rational& y)
{
  Rational r; mul(x, y, r); return r;
}

inline Rational operator / (const Rational& x, const Rational& y)
{
  Rational r; div(x, y, r); return r;
}
#endif
#endif

#endif