2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2015 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"
26 #include "string-convert.hh"
27 #include "libc-extension.hh"
32 Rational::to_double () const
34 if (sign_ == -1 || sign_ == 1 || sign_ == 0)
35 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)
105 Rational::set_infinite (int s)
107 sign_ = ::sign (s) * 2;
112 Rational::operator - () const
120 Rational::div_rat (Rational div) const
124 return r.trunc_rat ();
128 Rational::mod_rat (Rational div) const
131 r = (r / div - r.div_rat (div)) * div;
136 copy & paste from scm_gcd (GUILE).
150 /* Determine a common factor 2^k */
151 while (!(1 & (u | v)))
157 /* Now, any factor 2^n can be eliminated */
182 Rational::normalize ()
201 I64 g = gcd (num_, den_);
208 Rational::sign () const
210 return ::sign (sign_);
214 Rational::compare (Rational const &r, Rational const &s)
216 if (r.sign_ < s.sign_)
218 else if (r.sign_ > s.sign_)
220 else if (r.is_infinity ()) // here s is also infinite with the same sign
222 else if (r.sign_ == 0) // here s.sign_ is also zero
224 return ::sign (r - s);
228 compare (Rational const &r, Rational const &s)
230 return Rational::compare (r, s);
234 Rational::operator %= (Rational r)
241 Rational::operator += (Rational r)
245 else if (r.is_infinity ())
249 I64 lcm = (den_ / gcd (r.den_, den_)) * r.den_;
250 I64 n = sign_ * num_ * (lcm / den_) + r.sign_ * r.num_ * (lcm / r.den_);
252 sign_ = ::sign (n) * ::sign (d);
261 copied from libg++ 2.8.0
263 Rational::Rational (double x)
271 double mantissa = frexp (x, &expt);
273 const int FACT = 1 << 20;
276 Thanks to Afie for this too simple idea.
278 do not blindly substitute by libg++ code, since that uses
279 arbitrary-size integers. The rationals would overflow too
283 num_ = (U64) (mantissa * FACT);
310 Rational::operator *= (Rational r)
312 sign_ *= ::sign (r.sign_);
313 if (r.is_infinity ())
328 Rational::operator /= (Rational r)
341 Rational::operator -= (Rational r)
348 Rational::to_string () const
352 string s (sign_ > 0 ? "" : "-");
353 return string (s + "infinity");
356 string s = ::to_string (num ());
357 if (den () != 1 && num ())
358 s += "/" + ::to_string (den ());
363 Rational::to_int () const
365 return (int) (num () / den ());
375 Rational::is_infinity () const
377 return sign_ == 2 || sign_ == -2;