X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=flower%2Frational.cc;h=724f0d99ca5b526dfc5f067decfbaf2066f7796c;hb=21c6ebc5dc4183ff9e922750fc3d3b2ebf2625f9;hp=7816905ea2c33baa732d25d5769ac9b1211a46e5;hpb=1226b640103812acf135e88002eb7506a9d6baad;p=lilypond.git diff --git a/flower/rational.cc b/flower/rational.cc index 7816905ea2..724f0d99ca 100644 --- a/flower/rational.cc +++ b/flower/rational.cc @@ -3,7 +3,7 @@ source file of the Flower Library - (c) 1997--2006 Han-Wen Nienhuys + (c) 1997--2007 Han-Wen Nienhuys */ #include "rational.hh" @@ -16,11 +16,11 @@ using namespace std; #include "string-convert.hh" #include "libc-extension.hh" -Rational::operator double () const +double +Rational::to_double () const { - if (sign_==-1 || sign_ == 1) + if (sign_ == -1 || sign_ == 1 || sign_ == 0) return ((double)sign_) * num_ / den_; - if (sign_ == -2) return -HUGE_VAL; else if (sign_ == 2) @@ -31,10 +31,6 @@ Rational::operator double () const return 0.0; } -Rational::operator bool () const -{ - return sign_ && num_; -} #ifdef STREAM_SUPPORT ostream & @@ -45,6 +41,12 @@ operator << (ostream &o, Rational r) } #endif +Rational +Rational::abs () const +{ + return Rational (num_, den_); +} + Rational Rational::trunc_rat () const { @@ -60,29 +62,18 @@ 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) @@ -114,8 +105,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_) { @@ -186,9 +225,9 @@ Rational::operator += (Rational r) 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; } @@ -218,19 +257,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 (); } } @@ -255,7 +294,7 @@ Rational::operator *= (Rational r) num_ *= r.num_; den_ *= r.den_; - normalise (); + normalize (); exit_func: return *this; }