Use out/gen-emmentaler-scripts, not gen-emmentaler-scripts.py.
[lilypond.git] / flower / rational.cc
blob4205b1d683042153a4fe41dfa6651bb343c75d70
1 /*
2 rational.cc -- implement Rational
4 source file of the Flower Library
6 (c) 1997--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
9 #include "rational.hh"
11 #include <cmath>
12 #include <cassert>
13 #include <cstdlib>
14 using namespace std;
16 #include "string-convert.hh"
17 #include "libc-extension.hh"
19 double
20 Rational::to_double () const
22 if (sign_ == -1 || sign_ == 1 || sign_ == 0)
23 return ((double)sign_) * num_ / den_;
24 if (sign_ == -2)
25 return -HUGE_VAL;
26 else if (sign_ == 2)
27 return HUGE_VAL;
28 else
29 assert (false);
31 return 0.0;
35 #ifdef STREAM_SUPPORT
36 ostream &
37 operator << (ostream &o, Rational r)
39 o << r.string ();
40 return o;
42 #endif
44 Rational
45 Rational::abs () const
47 return Rational (num_, den_);
50 Rational
51 Rational::trunc_rat () const
53 if (is_infinity())
54 return *this;
55 return Rational ((num_ - (num_ % den_)) * sign_, den_);
58 Rational::Rational ()
60 sign_ = 0;
61 num_ = den_ = 1;
64 Rational::Rational (int n, int d)
66 sign_ = ::sign (n) * ::sign (d);
67 num_ = ::abs (n);
68 den_ = ::abs (d);
69 normalize ();
72 Rational::Rational (int n)
74 sign_ = ::sign (n);
75 num_ = ::abs (n);
76 den_ = 1;
80 void
81 Rational::set_infinite (int s)
83 sign_ = ::sign (s) * 2;
84 num_ = 1;
87 Rational
88 Rational::operator - () const
90 Rational r (*this);
91 r.negate ();
92 return r;
95 Rational
96 Rational::div_rat (Rational div) const
98 Rational r (*this);
99 r /= div;
100 return r.trunc_rat ();
103 Rational
104 Rational::mod_rat (Rational div) const
106 Rational r (*this);
107 r = (r / div - r.div_rat (div)) * div;
108 return r;
113 copy & paste from scm_gcd (GUILE).
115 static int
116 gcd (long u, long v)
118 long result = 0;
119 if (u == 0)
120 result = v;
121 else if (v == 0)
122 result = u;
123 else
125 long k = 1;
126 long t;
127 /* Determine a common factor 2^k */
128 while (!(1 & (u | v)))
130 k <<= 1;
131 u >>= 1;
132 v >>= 1;
134 /* Now, any factor 2^n can be eliminated */
135 if (u & 1)
136 t = -v;
137 else
139 t = u;
141 t = t >> 1;
143 if (!(1 & t))
144 goto b3;
145 if (t > 0)
146 u = t;
147 else
148 v = -t;
149 t = u - v;
150 if (t != 0)
151 goto b3;
152 result = u * k;
155 return result;
159 void
160 Rational::normalize ()
162 if (!sign_)
164 den_ = 1;
165 num_ = 0;
167 else if (!den_)
169 sign_ = 2;
170 num_ = 1;
172 else if (!num_)
174 sign_ = 0;
175 den_ = 1;
177 else
179 int g = gcd (num_, den_);
181 num_ /= g;
182 den_ /= g;
186 Rational::sign () const
188 return ::sign (sign_);
192 Rational::compare (Rational const &r, Rational const &s)
194 if (r.sign_ < s.sign_)
195 return -1;
196 else if (r.sign_ > s.sign_)
197 return 1;
198 else if (r.is_infinity ())
199 return 0;
200 else if (r.sign_ == 0)
201 return 0;
202 return r.sign_ * ::sign (int (r.num_ * s.den_) - int (s.num_ * r.den_));
206 compare (Rational const &r, Rational const &s)
208 return Rational::compare (r, s);
211 Rational &
212 Rational::operator %= (Rational r)
214 *this = mod_rat (r);
215 return *this;
218 Rational &
219 Rational::operator += (Rational r)
221 if (is_infinity ())
223 else if (r.is_infinity ())
224 *this = r;
225 else
227 int lcm = (den_ / gcd (r.den_, den_)) * r.den_;
228 int n = sign_ * num_ * (lcm / den_) + r.sign_ * r.num_ * (lcm / r.den_);
229 int d = lcm;
230 sign_ = ::sign (n) * ::sign (d);
231 num_ = ::abs (n);
232 den_ = ::abs (d);
233 normalize ();
235 return *this;
239 copied from libg++ 2.8.0
241 Rational::Rational (double x)
243 if (x != 0.0)
245 sign_ = ::sign (x);
246 x *= sign_;
248 int expt;
249 double mantissa = frexp (x, &expt);
251 const int FACT = 1 << 20;
254 Thanks to Afie for this too simple idea.
256 do not blindly substitute by libg++ code, since that uses
257 arbitrary-size integers. The rationals would overflow too
258 easily.
261 num_ = (unsigned int) (mantissa * FACT);
262 den_ = (unsigned int) FACT;
263 normalize ();
264 if (expt < 0)
265 den_ <<= -expt;
266 else
267 num_ <<= expt;
268 normalize ();
270 else
272 num_ = 0;
273 den_ = 1;
274 sign_ = 0;
275 normalize ();
279 void
280 Rational::invert ()
282 int r (num_);
283 num_ = den_;
284 den_ = r;
287 Rational &
288 Rational::operator *= (Rational r)
290 sign_ *= ::sign (r.sign_);
291 if (r.is_infinity ())
293 sign_ = sign () * 2;
294 goto exit_func;
297 num_ *= r.num_;
298 den_ *= r.den_;
300 normalize ();
301 exit_func:
302 return *this;
305 Rational &
306 Rational::operator /= (Rational r)
308 r.invert ();
309 return (*this *= r);
312 void
313 Rational::negate ()
315 sign_ *= -1;
318 Rational &
319 Rational::operator -= (Rational r)
321 r.negate ();
322 return (*this += r);
325 string
326 Rational::to_string () const
328 if (is_infinity ())
330 string s (sign_ > 0 ? "" : "-");
331 return string (s + "infinity");
334 string s = ::to_string (num ());
335 if (den () != 1 && num ())
336 s += "/" + ::to_string (den ());
337 return s;
341 Rational::to_int () const
343 return (int) num () / den ();
347 sign (Rational r)
349 return r.sign ();
352 bool
353 Rational::is_infinity () const
355 return sign_ == 2 || sign_ == -2;