2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2014 Han-Wen Nienhuys <hanwen@xs4all.nl>
6 LilyPond is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 LilyPond is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
20 #include "rational.hh"
27 #include "string-convert.hh"
28 #include "libc-extension.hh"
31 Rational::to_double () const
33 if (sign_ == -1 || sign_ == 1 || sign_ == 0)
34 return (double)sign_ * (double)num_ / (double)den_;
47 operator << (ostream &o, Rational r)
55 Rational::abs () const
57 return Rational (num_, den_);
61 Rational::trunc_rat () const
65 return Rational ((num_ - (num_ % den_)) * sign_, den_);
74 Rational::Rational (I64 n, I64 d)
76 sign_ = ::sign (n) * ::sign (d);
82 Rational::Rational (I64 n)
89 Rational::Rational (U64 n)
96 Rational::Rational (int n)
104 Rational::set_infinite (int s)
106 sign_ = ::sign (s) * 2;
111 Rational::operator - () const
119 Rational::div_rat (Rational div) const
123 return r.trunc_rat ();
127 Rational::mod_rat (Rational div) const
130 r = (r / div - r.div_rat (div)) * div;
135 copy & paste from scm_gcd (GUILE).
149 /* Determine a common factor 2^k */
150 while (!(1 & (u | v)))
156 /* Now, any factor 2^n can be eliminated */
181 Rational::normalize ()
200 I64 g = gcd (num_, den_);
207 Rational::sign () const
209 return ::sign (sign_);
213 Rational::compare (Rational const &r, Rational const &s)
215 if (r.sign_ < s.sign_)
217 else if (r.sign_ > s.sign_)
219 else if (r.is_infinity ())
221 else if (r.sign_ == 0)
223 return r.sign_ * ::sign ((I64) (r.num_ * s.den_) - (I64) (s.num_ * r.den_));
227 compare (Rational const &r, Rational const &s)
229 return Rational::compare (r, s);
233 Rational::operator %= (Rational r)
240 Rational::operator += (Rational r)
244 else if (r.is_infinity ())
248 I64 lcm = (den_ / gcd (r.den_, den_)) * r.den_;
249 I64 n = sign_ * num_ * (lcm / den_) + r.sign_ * r.num_ * (lcm / r.den_);
251 sign_ = ::sign (n) * ::sign (d);
260 copied from libg++ 2.8.0
262 Rational::Rational (double x)
270 double mantissa = frexp (x, &expt);
272 const int FACT = 1 << 20;
275 Thanks to Afie for this too simple idea.
277 do not blindly substitute by libg++ code, since that uses
278 arbitrary-size integers. The rationals would overflow too
282 num_ = (U64) (mantissa * FACT);
309 Rational::operator *= (Rational r)
311 sign_ *= ::sign (r.sign_);
312 if (r.is_infinity ())
327 Rational::operator /= (Rational r)
340 Rational::operator -= (Rational r)
347 Rational::to_string () const
351 string s (sign_ > 0 ? "" : "-");
352 return string (s + "infinity");
355 string s = ::to_string (num ());
356 if (den () != 1 && num ())
357 s += "/" + ::to_string (den ());
362 Rational::to_int () const
364 return (int) (num () / den ());
374 Rational::is_infinity () const
376 return sign_ == 2 || sign_ == -2;