File: frac.cc

package info (click to toggle)
epix 1.2.10-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 3,160 kB
  • sloc: cpp: 16,442; sh: 5,058; makefile: 198
file content (262 lines) | stat: -rw-r--r-- 5,126 bytes parent folder | download | duplicates (4)
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
/* 
 * frac.cc -- ePiX rational number class
 *
 * This file is part of ePiX, a C++ library for creating high-quality 
 * figures in LaTeX 
 *
 * Version 1.2.0-2
 * Last Change: September 26, 2007
 */

/* 
 * Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
 * Andrew D. Hwang <rot 13 nujnat at zngupf dot ubylpebff dot rqh>
 * Department of Mathematics and Computer Science
 * College of the Holy Cross
 * Worcester, MA, 01610-2395, USA
 */

/*
 * ePiX is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * ePiX is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
 * License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with ePiX; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
#include <cmath>

#include "frac.h"

namespace ePiX {

  const int MAX_DENOM(10000);
  const double EPS(1.0/MAX_DENOM);

  unsigned int __epix_gcd (int i, unsigned int j);

  frac::frac(const int n, const unsigned int d)
    : m_num(n), m_denom(d) { }

  // express arg as a fraction with denominator no larger than MAX_DENOM
  frac::frac(double arg)
  {
    unsigned int denom(1);
    if (fabs(arg) < EPS)
      {
	m_num = 0;
	m_denom = 1;
      }

    else
      {
	int sgn(arg < 0 ? -1 : 1);
	double abs_arg(sgn*arg);

	// store best approximation
	int best_num(0);
	double running_error(0.5);

	while (denom <= 1+MAX_DENOM)
	  {
	    const double tmp(abs_arg*denom);

	    int tmp_lo((int) floor(tmp));

	    if (tmp - tmp_lo < EPS) // good approx
	      {
		best_num = tmp_lo;
		break;
	      }

	    else if (tmp - tmp_lo < running_error) // improved approx
	      {
		best_num = tmp_lo;
		running_error = tmp - best_num;
	      }

	    int tmp_hi((int) ceil(tmp));
	    if (tmp_hi - tmp < EPS)
	      {
		best_num = tmp_hi;
		break;
	      }

	  else if (tmp_hi - tmp < running_error)
	    {
	      best_num = tmp_hi;
	      running_error = best_num - tmp;
	    }

	    ++denom;
	  }

	m_num = sgn*best_num;
	m_denom = denom;
      }
  } // end of frac::frac(double arg)


  // increment operators
  frac& frac::operator += (const frac& arg)
  {
    m_num *= arg.m_denom;
    m_num += m_denom*arg.m_num;
    m_denom *= arg.m_denom;

    return *this;
  }

  frac& frac::operator -= (const frac& arg)
  {
    m_num *= arg.m_denom;
    m_num -= m_denom*arg.m_num;
    m_denom *= arg.m_denom;

    return *this;
  }

  frac& frac::operator *= (const frac& arg)
  {
    m_num *= arg.m_num;
    m_denom *= arg.m_denom;

    return *this;
  }

  frac& frac::operator /= (const frac& arg)
  {
    unsigned int arg_num(arg.m_num < 0 ? -arg.m_num : arg.m_num);

    m_num *= arg.m_denom;
    m_denom *= arg_num;

    if (arg.m_num < 0)
      m_num = -1;

    return *this;
  }

  frac& frac::reduce()
  {
    unsigned int factor(__epix_gcd(m_num, m_denom));
    m_num /= factor;
    m_denom /= factor;

    return *this;
  }

  double frac::eval() const
  {
    double temp(m_num);
    return temp /= m_denom;
  }

  int frac::num() const
  {
    return m_num;
  }

  unsigned int frac::denom() const
  {
    return m_denom;
  }

  bool frac::is_int() const
  {
    return __epix_gcd(m_num, m_denom) == m_denom;
  }


  frac operator+ (frac arg1, const frac& arg2)
  {
    return arg1 += arg2;
  }

  frac operator- (frac arg1)
  {
    return arg1 *= -1;
  }

  frac operator- (frac arg1, const frac& arg2)
  {
    return arg1 -= arg2;
  }

  frac operator* (frac arg1, const frac& arg2)
  {
    return arg1 *= arg2;
  }

  frac operator/ (frac arg1, const frac& arg2)
  {
    return arg1 /= arg2;
  }

  // (in)equality
  bool operator == (const frac& u, const frac& v)
  {
    return u.num()*v.denom() == v.num()*u.denom();
  }

  bool operator != (const frac& u, const frac& v)
  {
    return !(u == v);
  }

  // denoms are unsigned
  bool operator < (const frac& u, const frac& v)
  {
    return u.num()*v.denom() < v.num()*u.denom();
  }

  bool operator > (const frac& u, const frac& v)
  {
    return u.num()*v.denom() > v.num()*u.denom();
  }

  bool operator <= (const frac& u, const frac& v)
  {
    return u.num()*v.denom() <= v.num()*u.denom();
  }

  bool operator >= (const frac& u, const frac& v)
  {
    return u.num()*v.denom() >= v.num()*u.denom();
  }

  // N.B.: gcd(0,i) = |i|
  unsigned int __epix_gcd (int i, unsigned int j)
  {
    unsigned int new_i(i<0 ? -i : i);
    unsigned int temp;

    if (new_i==0 || j==0) // (1,0) and (0,1) coprime, others not
      return new_i + j;

    else {
      if (j < new_i) // swap them
	{
	  temp = j;
	  j = new_i;
	  new_i = temp;
	}
      // Euclidean algorithm
      while ((temp = j%new_i)) // i does not evenly divide j
	{
	  j = new_i;
	  new_i = temp;
	}
    
      return new_i;
    }
  }
} // end of namespace