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"
27 #include "string-convert.hh"
28 #include "libc-extension.hh"
33 Rational::to_double () const
35 if (sign_ == -1 || sign_ == 1 || sign_ == 0)
36 return (double)sign_ * (double)num_ / (double)den_;
49 operator << (ostream &o, Rational r)
57 Rational::abs () const
59 return Rational (num_, den_);
63 Rational::trunc_rat () const
67 return Rational ((num_ - (num_ % den_)) * sign_, den_);
76 Rational::Rational (I64 n, I64 d)
78 sign_ = ::sign (n) * ::sign (d);
84 Rational::Rational (I64 n)
91 Rational::Rational (U64 n)
98 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;
137 copy & paste from scm_gcd (GUILE).
151 /* Determine a common factor 2^k */
152 while (!(1 & (u | v)))
158 /* Now, any factor 2^n can be eliminated */
183 Rational::normalize ()
202 I64 g = gcd (num_, den_);
209 Rational::sign () const
211 return ::sign (sign_);
215 Rational::compare (Rational const &r, Rational const &s)
217 if (r.sign_ < s.sign_)
219 else if (r.sign_ > s.sign_)
221 else if (r.is_infinity ()) // here s is also infinite with the same sign
223 else if (r.sign_ == 0) // here s.sign_ is also zero
225 return ::sign (r - s);
229 compare (Rational const &r, Rational const &s)
231 return Rational::compare (r, s);
235 Rational::operator %= (Rational r)
242 Rational::operator += (Rational r)
246 else if (r.is_infinity ())
250 I64 lcm = (den_ / gcd (r.den_, den_)) * r.den_;
251 I64 n = sign_ * num_ * (lcm / den_) + r.sign_ * r.num_ * (lcm / r.den_);
253 sign_ = ::sign (n) * ::sign (d);
262 copied from libg++ 2.8.0
264 Rational::Rational (double x)
272 double mantissa = frexp (x, &expt);
274 const int FACT = 1 << 20;
277 Thanks to Afie for this too simple idea.
279 do not blindly substitute by libg++ code, since that uses
280 arbitrary-size integers. The rationals would overflow too
284 num_ = (U64) (mantissa * FACT);
311 Rational::operator *= (Rational r)
313 sign_ *= ::sign (r.sign_);
314 if (r.is_infinity ())
329 Rational::operator /= (Rational r)
342 Rational::operator -= (Rational r)
349 Rational::to_string () const
353 string s (sign_ > 0 ? "" : "-");
354 return string (s + "infinity");
357 string s = ::to_string (num ());
358 if (den () != 1 && num ())
359 s += "/" + ::to_string (den ());
364 Rational::to_int () const
366 return (int) (num () / den ());
376 Rational::is_infinity () const
378 return sign_ == 2 || sign_ == -2;