2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2011 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_;
48 operator << (ostream &o, Rational r)
56 Rational::abs () const
58 return Rational (num_, den_);
62 Rational::trunc_rat () const
66 return Rational ((num_ - (num_ % den_)) * sign_, den_);
75 Rational::Rational (I64 n, I64 d)
77 sign_ = ::sign (n) * ::sign (d);
83 Rational::Rational (I64 n)
90 Rational::Rational (U64 n)
97 Rational::Rational (int n)
106 Rational::set_infinite (int s)
108 sign_ = ::sign (s) * 2;
113 Rational::operator - () const
121 Rational::div_rat (Rational div) const
125 return r.trunc_rat ();
129 Rational::mod_rat (Rational div) const
132 r = (r / div - r.div_rat (div)) * div;
138 copy & paste from scm_gcd (GUILE).
152 /* Determine a common factor 2^k */
153 while (!(1 & (u | v)))
159 /* Now, any factor 2^n can be eliminated */
185 Rational::normalize ()
204 I64 g = gcd (num_, den_);
211 Rational::sign () const
213 return ::sign (sign_);
217 Rational::compare (Rational const &r, Rational const &s)
219 if (r.sign_ < s.sign_)
221 else if (r.sign_ > s.sign_)
223 else if (r.is_infinity ())
225 else if (r.sign_ == 0)
227 return r.sign_ * ::sign ((I64) (r.num_ * s.den_) - (I64) (s.num_ * r.den_));
231 compare (Rational const &r, Rational const &s)
233 return Rational::compare (r, s);
237 Rational::operator %= (Rational r)
244 Rational::operator += (Rational r)
248 else if (r.is_infinity ())
252 I64 lcm = (den_ / gcd (r.den_, den_)) * r.den_;
253 I64 n = sign_ * num_ * (lcm / den_) + r.sign_ * r.num_ * (lcm / r.den_);
255 sign_ = ::sign (n) * ::sign (d);
264 copied from libg++ 2.8.0
266 Rational::Rational (double x)
274 double mantissa = frexp (x, &expt);
276 const int FACT = 1 << 20;
279 Thanks to Afie for this too simple idea.
281 do not blindly substitute by libg++ code, since that uses
282 arbitrary-size integers. The rationals would overflow too
286 num_ = (U64) (mantissa * FACT);
313 Rational::operator *= (Rational r)
315 sign_ *= ::sign (r.sign_);
316 if (r.is_infinity ())
331 Rational::operator /= (Rational r)
344 Rational::operator -= (Rational r)
351 Rational::to_string () const
355 string s (sign_ > 0 ? "" : "-");
356 return string (s + "infinity");
359 string s = ::to_string (num ());
360 if (den () != 1 && num ())
361 s += "/" + ::to_string (den ());
366 Rational::to_int () const
368 return (int)(num () / den ());
378 Rational::is_infinity () const
380 return sign_ == 2 || sign_ == -2;