X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=flower%2Frational.cc;h=724f0d99ca5b526dfc5f067decfbaf2066f7796c;hb=21c6ebc5dc4183ff9e922750fc3d3b2ebf2625f9;hp=df18b4cbcd57cf5b0ecd2d8368d36d667e576698;hpb=77add406027ae5afda409a2e77b35852c5fae4f9;p=lilypond.git diff --git a/flower/rational.cc b/flower/rational.cc index df18b4cbcd..724f0d99ca 100644 --- a/flower/rational.cc +++ b/flower/rational.cc @@ -1,125 +1,190 @@ /* rational.cc -- implement Rational - + source file of the Flower Library - (c) 1997 Han-Wen Nienhuys + (c) 1997--2007 Han-Wen Nienhuys */ -#include + #include "rational.hh" -#include "string.hh" -#include "string-convert.hh" -#include "libc-extension.hh" -Rational::operator bool () const -{ - return sign_; -} +#include +#include +#include +using namespace std; -Rational::operator int () const -{ - return sign_ * num_ / den_; -} +#include "string-convert.hh" +#include "libc-extension.hh" -Rational::operator double () const +double +Rational::to_double () const { - return (double)sign_ * num_ / den_; + if (sign_ == -1 || sign_ == 1 || sign_ == 0) + return ((double)sign_) * num_ / den_; + if (sign_ == -2) + return -HUGE_VAL; + else if (sign_ == 2) + return HUGE_VAL; + else + assert (false); + + return 0.0; } + +#ifdef STREAM_SUPPORT ostream & operator << (ostream &o, Rational r) { - o << r.str (); + o << r.string (); return o; } +#endif - +Rational +Rational::abs () const +{ + return Rational (num_, den_); +} Rational -Rational::truncated () const +Rational::trunc_rat () const { - return Rational(num_ - (num_ % den_), den_); + return Rational (num_ - (num_ % den_), den_); } Rational::Rational () { - sign_ = 1; + sign_ = 0; num_ = den_ = 1; } Rational::Rational (int n, int d) { sign_ = ::sign (n) * ::sign (d); - num_ = abs (n); - den_ = abs (d); - normalise (); + num_ = ::abs (n); + den_ = ::abs (d); + normalize (); } - -static -int gcd (int a, int b) +Rational::Rational (int n) { - int t; - while ((t = a % b)) - { - a = b; - b = t; - } - return b; + sign_ = ::sign (n); + num_ = ::abs (n); + den_ = 1; } -static -int lcm (int a, int b) -{ - return abs (a*b / gcd (a,b)); -} void Rational::set_infinite (int s) { - sign_ = ::sign (s) * 2; + sign_ = ::sign (s) * 2; } Rational Rational::operator - () const { - Rational r(*this); + Rational r (*this); r.negate (); return r; } +Rational +Rational::div_rat (Rational div) const +{ + Rational r (*this); + r /= div; + return r.trunc_rat (); +} + +Rational +Rational::mod_rat (Rational div) const +{ + Rational r (*this); + r = (r / div - r.div_rat (div)) * div; + return r; +} + + +/* + copy & paste from scm_gcd (GUILE). + */ +static int +gcd (long u, long v) +{ + long result = 0; + if (u == 0) + result = v; + else if (v == 0) + result = u; + else + { + long k = 1; + long t; + /* Determine a common factor 2^k */ + while (!(1 & (u | v))) + { + k <<= 1; + u >>= 1; + v >>= 1; + } + /* Now, any factor 2^n can be eliminated */ + if (u & 1) + t = -v; + else + { + t = u; + b3: + t = t >> 1; + } + if (!(1 & t)) + goto b3; + if (t > 0) + u = t; + else + v = -t; + t = u - v; + if (t != 0) + goto b3; + result = u * k; + } + + return result; +} + + void -Rational::normalise () +Rational::normalize () { if (!sign_) { den_ = 1; num_ = 0; - return ; } - if (!den_) - sign_ = 2; - if (!num_) - sign_ = 0; - - int g = gcd (num_ , den_); + else if (!den_) + { + sign_ = 2; + num_ = 1; + } + else if (!num_) + { + sign_ = 0; + den_ = 1; + } + else + { + int g = gcd (num_, den_); - num_ /= g; - den_ /= g; + num_ /= g; + den_ /= g; + } } - int Rational::sign () const { return ::sign (sign_); } -bool -Rational::infty_b () const -{ - return abs (sign_) > 1; -} - int Rational::compare (Rational const &r, Rational const &s) { @@ -127,85 +192,92 @@ Rational::compare (Rational const &r, Rational const &s) return -1; else if (r.sign_ > s.sign_) return 1; - else if (r.infty_b ()) + else if (r.is_infinity ()) return 0; - - return (r - s).sign (); + else if (r.sign_ == 0) + return 0; + return r.sign_ * ::sign (int (r.num_ * s.den_) - int (s.num_ * r.den_)); } int compare (Rational const &r, Rational const &s) { - return Rational::compare (r, s ); + return Rational::compare (r, s); +} + +Rational & +Rational::operator %= (Rational r) +{ + *this = mod_rat (r); + return *this; } Rational & Rational::operator += (Rational r) { - if (infty_b ()) + if (is_infinity ()) ; - else if (r.infty_b ()) + else if (r.is_infinity ()) + *this = r; + else { - *this = r; - } - else - { - int n = sign_ * num_ *r.den_ + r.sign_ * den_ * r.num_; - int d = den_ * r.den_; - sign_ = ::sign (n) * ::sign(d); - num_ = abs (n); - den_ = abs (d); - normalise (); + 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; + sign_ = ::sign (n) * ::sign (d); + num_ = ::abs (n); + den_ = ::abs (d); + normalize (); } return *this; } - /* copied from libg++ 2.8.0 - */ -Rational::Rational(double x) +*/ +Rational::Rational (double x) { - num_ = 0; - den_ = 1; if (x != 0.0) { sign_ = ::sign (x); x *= sign_; - const long shift = 15; // a safe shift per step - const double width = 32768.0; // = 2^shift - const int maxiter = 20; // ought not be necessary, but just in case, - // max 300 bits of precision int expt; - double mantissa = frexp(x, &expt); - long exponent = expt; - double intpart; - int k = 0; - while (mantissa != 0.0 && k++ < maxiter) - { - mantissa *= width; - mantissa = modf(mantissa, &intpart); - num_ <<= shift; - num_ += (long)intpart; - exponent -= shift; - } - if (exponent > 0) - num_ <<= exponent; - else if (exponent < 0) - den_ <<= -exponent; - } else { - sign_ = 0; + double mantissa = frexp (x, &expt); + + const int FACT = 1 << 20; + + /* + Thanks to Afie for this too simple idea. + + do not blindly substitute by libg++ code, since that uses + arbitrary-size integers. The rationals would overflow too + easily. + */ + + num_ = (unsigned int) (mantissa * FACT); + den_ = (unsigned int) FACT; + normalize (); + if (expt < 0) + den_ <<= -expt; + else + num_ <<= expt; + normalize (); + } + else + { + num_ = 0; + den_ = 1; + sign_ = 0; + normalize (); } - normalise(); } - void Rational::invert () { int r (num_); - num_ = den_; + num_ = den_; den_ = r; } @@ -213,8 +285,8 @@ Rational & Rational::operator *= (Rational r) { sign_ *= ::sign (r.sign_); - if (r.infty_b ()) - { + if (r.is_infinity ()) + { sign_ = sign () * 2; goto exit_func; } @@ -222,11 +294,11 @@ Rational::operator *= (Rational r) num_ *= r.num_; den_ *= r.den_; - normalise (); + normalize (); exit_func: return *this; } - + Rational & Rational::operator /= (Rational r) { @@ -240,49 +312,42 @@ Rational::negate () sign_ *= -1; } -Rational& +Rational & Rational::operator -= (Rational r) { r.negate (); return (*this += r); } -/* - be paranoid about overiding libg++ stuff - */ -Rational & -Rational::operator = (Rational const &r) -{ - copy (r); - return *this; -} - -Rational::Rational (Rational const &r) -{ - copy (r); -} - -Rational::operator String () const +string +Rational::to_string () const { - return str (); -} - -String -Rational::str () const -{ - if (infty_b ()) + if (is_infinity ()) { - String s (sign_ > 0 ? "" : "-" ); - return String (s + "infinity"); + string s (sign_ > 0 ? "" : "-"); + return string (s + "infinity"); } - String s (num ()); + + string s = ::to_string (num ()); if (den () != 1 && num ()) - s += "/" + String (den ()); + s += "/" + ::to_string (den ()); return s; } +int +Rational::to_int () const +{ + return (int) num () / den (); +} + int sign (Rational r) { return r.sign (); } + +bool +Rational::is_infinity () const +{ + return sign_ == 2 || sign_ == -2; +}