]> git.donarmstrong.com Git - lilypond.git/blobdiff - flower/rational.cc
patch::: 1.5.5.jcn1
[lilypond.git] / flower / rational.cc
index df18b4cbcd57cf5b0ecd2d8368d36d667e576698..d5e679a9e973e0711a18ce443986f2ab51e9e9e1 100644 (file)
@@ -3,24 +3,15 @@
   
   source file of the Flower Library
 
-  (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
+  (c)  1997--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
 */
+#include <math.h>
 #include <stdlib.h>
 #include "rational.hh"
 #include "string.hh"
 #include "string-convert.hh"  
 #include "libc-extension.hh"
 
-Rational::operator bool () const
-{
-  return sign_;
-}
-
-Rational::operator int () const
-{
-  return sign_ * num_ / den_;
-}
-
 Rational::operator double () const
 {
   return (double)sign_ * num_ / den_;
@@ -33,17 +24,15 @@ operator << (ostream &o, Rational r)
   return o;
 }
 
-
-
 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;
 }
 
@@ -55,7 +44,6 @@ Rational::Rational (int n, int d)
   normalise ();
 }
 
-
 static
 int gcd (int a, int b)
 {
@@ -83,11 +71,27 @@ Rational::set_infinite (int s)
 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;
+}
+
 void
 Rational::normalise ()
 {
@@ -130,7 +134,7 @@ Rational::compare (Rational const &r, Rational const &s)
   else if (r.infty_b ())
     return 0;
 
-  return  (r - s).sign ();
+  return (r - s).sign ();
 }
 
 int
@@ -139,6 +143,13 @@ compare (Rational const &r, Rational const &s)
   return Rational::compare (r, s );
 }
 
+Rational &
+Rational::operator %= (Rational r)
+{
+  *this = r.mod_rat (r);
+  return *this;
+}
+
 Rational &
 Rational::operator += (Rational r)
 {
@@ -152,7 +163,7 @@ Rational::operator += (Rational r)
     {
       int n = sign_ * num_ *r.den_ + r.sign_ * den_ * r.num_;
       int d = den_ * r.den_;
-      sign_ =  ::sign (n) * ::sign(d);
+      sign_ =  ::sign (n) * ::sign (d);
       num_ = abs (n);
       den_ = abs (d);
       normalise ();
@@ -164,40 +175,42 @@ Rational::operator += (Rational r)
 /*
   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;
+      normalise ();      
+      if (expt < 0)
+       den_ <<= -expt;
+      else
+       num_ <<= expt;
+      normalise ();
+    }
+  else
+    {
+      num_ = 0;
+      den_ = 1;
+      sign_ =0;
+      normalise ();
     }
-  normalise();
 }
 
 
@@ -257,16 +270,6 @@ Rational::operator = (Rational const &r)
   return *this;
 }
 
-Rational::Rational (Rational const &r)
-{
-  copy (r);
-}
-
-Rational::operator String () const
-{
-  return str ();
-}
-
 String
 Rational::str () const
 {
@@ -275,9 +278,9 @@ Rational::str () const
       String s (sign_ > 0 ? "" : "-" );
       return String (s + "infinity");
     }
-  String s (num ());
+  String s = to_str (num ());
   if (den () != 1 && num ())
-    s += "/" + String (den ());
+    s += "/" + to_str (den ());
   return s;
 }