X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=flower%2Frational.cc;h=4205b1d683042153a4fe41dfa6651bb343c75d70;hb=4aa112b9a32be19ee8c5987df4bd733df27ebe92;hp=9ab8dcc4cdce191bebf014fc18a2f5028bc1108b;hpb=ef48d78cf655c5ab12b707c5f2d5ae423564da9a;p=lilypond.git diff --git a/flower/rational.cc b/flower/rational.cc index 9ab8dcc4cd..4205b1d683 100644 --- a/flower/rational.cc +++ b/flower/rational.cc @@ -3,25 +3,32 @@ source file of the Flower Library - (c) 1997--2005 Han-Wen Nienhuys + (c) 1997--2007 Han-Wen Nienhuys */ #include "rational.hh" #include +#include #include +using namespace std; #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); -Rational::operator bool () const -{ - return sign_ && num_; + return 0.0; } @@ -34,10 +41,18 @@ operator << (ostream &o, Rational r) } #endif +Rational +Rational::abs () const +{ + return Rational (num_, den_); +} + 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 () @@ -49,34 +64,24 @@ Rational::Rational () 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 (); } Rational::Rational (int n) { sign_ = ::sign (n); - num_ = abs (n); + num_ = ::abs (n); den_ = 1; } -static inline -int gcd (int a, int b) -{ - int t; - while ((t = a % b)) - { - a = b; - b = t; - } - return b; -} void Rational::set_infinite (int s) { sign_ = ::sign (s) * 2; + num_ = 1; } Rational @@ -103,8 +108,56 @@ Rational::mod_rat (Rational div) const 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_) { @@ -171,13 +224,13 @@ Rational::operator += (Rational r) *this = r; else { - int lcm = (den_ / gcd (r.den_, den_)) * r.den_; + 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); - normalise (); + num_ = ::abs (n); + den_ = ::abs (d); + normalize (); } return *this; } @@ -207,19 +260,19 @@ Rational::Rational (double x) num_ = (unsigned int) (mantissa * FACT); den_ = (unsigned int) FACT; - normalise (); + normalize (); if (expt < 0) den_ <<= -expt; else num_ <<= expt; - normalise (); + normalize (); } else { num_ = 0; den_ = 1; sign_ = 0; - normalise (); + normalize (); } } @@ -244,7 +297,7 @@ Rational::operator *= (Rational r) num_ *= r.num_; den_ *= r.den_; - normalise (); + normalize (); exit_func: return *this; } @@ -269,16 +322,16 @@ Rational::operator -= (Rational r) return (*this += r); } -String +string Rational::to_string () const { if (is_infinity ()) { - String s (sign_ > 0 ? "" : "-"); - return String (s + "infinity"); + string s (sign_ > 0 ? "" : "-"); + return string (s + "infinity"); } - String s = ::to_string (num ()); + string s = ::to_string (num ()); if (den () != 1 && num ()) s += "/" + ::to_string (den ()); return s; @@ -287,7 +340,7 @@ Rational::to_string () const int Rational::to_int () const { - return num () / den (); + return (int) num () / den (); } int