]> git.donarmstrong.com Git - lilypond.git/blobdiff - flower/rational.cc
strip Processing `foo' from log file
[lilypond.git] / flower / rational.cc
index 748a8698413e2b86a3569a0a25731e2bf70c7067..724f0d99ca5b526dfc5f067decfbaf2066f7796c 100644 (file)
@@ -3,7 +3,7 @@
 
   source file of the Flower Library
 
-  (c) 1997--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
+  (c) 1997--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
 */
 
 #include "rational.hh"
@@ -16,7 +16,8 @@ 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 || sign_ == 0)
     return ((double)sign_) * num_ / den_;
@@ -74,21 +75,6 @@ Rational::Rational (int n)
 }
 
 
-/*
-  We can actually do a little better. See Knuth 4.5.2
- */
-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)
 {
@@ -119,6 +105,54 @@ 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::normalize ()
 {