/*
- rational.cc -- implement Rational
+ This file is part of LilyPond, the GNU music typesetter.
- source file of the Flower Library
+ Copyright (C) 1997--2011 Han-Wen Nienhuys <hanwen@xs4all.nl>
- (c) 1997--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
+ LilyPond is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ LilyPond is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
*/
#include "rational.hh"
Rational::to_double () const
{
if (sign_ == -1 || sign_ == 1 || sign_ == 0)
- return ((double)sign_) * num_ / den_;
+ return (double)sign_ * (double)num_ / (double)den_;
if (sign_ == -2)
return -HUGE_VAL;
else if (sign_ == 2)
Rational
Rational::trunc_rat () const
{
- return Rational (num_ - (num_ % den_), den_);
+ if (is_infinity())
+ return *this;
+ return Rational ((num_ - (num_ % den_)) * sign_, den_);
}
Rational::Rational ()
num_ = den_ = 1;
}
-Rational::Rational (int n, int d)
+Rational::Rational (I64 n, I64 d)
{
sign_ = ::sign (n) * ::sign (d);
num_ = ::abs (n);
normalize ();
}
+Rational::Rational (I64 n)
+{
+ sign_ = ::sign (n);
+ num_ = ::abs (n);
+ den_ = 1;
+}
+
+Rational::Rational (U64 n)
+{
+ sign_ = 1;
+ num_ = n;
+ den_ = 1;
+}
+
Rational::Rational (int n)
{
sign_ = ::sign (n);
Rational::set_infinite (int s)
{
sign_ = ::sign (s) * 2;
+ num_ = 1;
}
Rational
/*
copy & paste from scm_gcd (GUILE).
*/
-static int
-gcd (long u, long v)
+static I64
+gcd (I64 u, I64 v)
{
- long result = 0;
+ I64 result = 0;
if (u == 0)
result = v;
else if (v == 0)
result = u;
else
{
- long k = 1;
- long t;
+ I64 k = 1;
+ I64 t;
/* Determine a common factor 2^k */
while (!(1 & (u | v)))
{
}
else
{
- int g = gcd (num_, den_);
+ I64 g = gcd (num_, den_);
num_ /= g;
den_ /= g;
return 0;
else if (r.sign_ == 0)
return 0;
- return r.sign_ * ::sign (int (r.num_ * s.den_) - int (s.num_ * r.den_));
+ return r.sign_ * ::sign ((I64) (r.num_ * s.den_) - (I64) (s.num_ * r.den_));
}
int
*this = r;
else
{
- int lcm = (den_ / gcd (r.den_, den_)) * r.den_;
- int n = sign_ * num_ * (lcm / den_) + r.sign_ * r.num_ * (lcm / r.den_);
- int d = lcm;
+ I64 lcm = (den_ / gcd (r.den_, den_)) * r.den_;
+ I64 n = sign_ * num_ * (lcm / den_) + r.sign_ * r.num_ * (lcm / r.den_);
+ I64 d = lcm;
sign_ = ::sign (n) * ::sign (d);
num_ = ::abs (n);
den_ = ::abs (d);
easily.
*/
- num_ = (unsigned int) (mantissa * FACT);
- den_ = (unsigned int) FACT;
+ num_ = (U64) (mantissa * FACT);
+ den_ = (U64) FACT;
normalize ();
if (expt < 0)
den_ <<= -expt;
void
Rational::invert ()
{
- int r (num_);
+ I64 r (num_);
num_ = den_;
den_ = r;
}
int
Rational::to_int () const
{
- return (int) num () / den ();
+ return (int)(num () / den ());
}
int