]> git.donarmstrong.com Git - lilypond.git/commitdiff
Use long long integers in Rational class.
authorNeil Puttock <n.puttock@gmail.com>
Fri, 12 Sep 2008 21:54:20 +0000 (22:54 +0100)
committerNeil Puttock <n.puttock@gmail.com>
Fri, 12 Sep 2008 21:54:20 +0000 (22:54 +0100)
Use typedef long long from flower-proto.hh.

flower/include/flower-proto.hh
flower/include/rational.hh
flower/include/std-string.hh
flower/rational.cc
flower/std-string.cc
lily/lily-guile.cc

index 4421a619d7d3513ba67ba37697a42bc5bfc4b5b2..6aec04e5d065baabc725205468158ca215fee76a 100644 (file)
@@ -1,5 +1,5 @@
 /*
-  fflower-proto.hh -- typenames in flowerlib
+  flower-proto.hh -- typenames in flowerlib
 
   (c) 1996--2007 Han-Wen Nienhuys
 */
@@ -10,6 +10,7 @@
 char const *flower_version_str0 ();
 
 typedef unsigned char Byte;
+typedef long long I64;
 struct String_convert;
 
 #include "std-string.hh"
@@ -35,7 +36,7 @@ typedef short I16;
 typedef unsigned short U16;
 typedef unsigned U32;
 typedef int I32;
-typedef long long I64;
+typedef unsigned long long U64;
 
 
 struct File_storage;
index 969d0121470db512f58f342046fd5293c735ad29..57bd23593b2475b265b320dcdde20d8e87edbd38 100644 (file)
@@ -9,6 +9,7 @@
 #ifndef RATIONAL_HH
 #define RATIONAL_HH
 
+#include "flower-proto.hh"
 #include "std-string.hh"
 #include <limits.h>
 
@@ -26,7 +27,7 @@ class Rational
      0 if *this is zero.
   */
   int sign_;
-  unsigned int num_, den_;
+  U64 num_, den_;
   void normalize ();
   void copy (Rational const &);
 
@@ -34,10 +35,10 @@ public:
   void set_infinite (int sign);
   bool is_infinity () const;
   void invert ();
-  int numerator () const { return sign_ * num_; }
-  int denominator () const { return den_; }
-  int num () const { return numerator (); }
-  int den () const { return denominator (); }
+  I64 numerator () const { return sign_ * num_; }
+  I64 denominator () const { return den_; }
+  I64 num () const { return numerator (); }
+  I64 den () const { return denominator (); }
 
   Rational trunc_rat () const;
   Rational div_rat (Rational) const;
@@ -55,7 +56,8 @@ public:
   */
   Rational ();
   Rational (int);
-  explicit Rational (int, int);
+  Rational (I64);
+  explicit Rational (I64, I64);
   explicit Rational (double);
   Rational (Rational const &r) { copy (r);}
   Rational &operator = (Rational const &r)
@@ -99,6 +101,6 @@ ostream &
 operator << (ostream &, Rational);
 #endif
 
-const Rational infinity_rat (INT_MAX);
+const Rational infinity_rat (LLONG_MAX);
 
 #endif // RATIONAL_HH
index d5933d8f99985075de4908f475dc5bd9cebb3363..4cff4e2e6045e7708a763a2005499deb4af74c39 100644 (file)
@@ -10,6 +10,7 @@
 #define STD_STRING_HH
  
 #include "compare.hh"
+#include "flower-proto.hh"
 
 #if 0
 /*
@@ -33,6 +34,7 @@ string to_string (int i, char const *format=0);
 string to_string (double f, char const *format=0);
 string to_string (long);
 string to_string (long unsigned);
+string to_string (I64, char const *format=0);
 string to_string (unsigned);
 string to_string (bool b);
 string to_string (char const *format, ...)
index 4205b1d683042153a4fe41dfa6651bb343c75d70..6046374630bf7e964de4a1c0070e6a44e49c54af 100644 (file)
@@ -61,7 +61,7 @@ Rational::Rational ()
   num_ = den_ = 1;
 }
 
-Rational::Rational (int n, int d)
+Rational::Rational (I64 n, I64 d)
 {
   sign_ = ::sign (n) * ::sign (d);
   num_ = ::abs (n);
@@ -69,6 +69,13 @@ Rational::Rational (int n, int d)
   normalize ();
 }
 
+Rational::Rational (I64 n)
+{
+  sign_ = ::sign (n);
+  num_ = ::abs (n);
+  den_ = 1;
+}
+
 Rational::Rational (int n)
 {
   sign_ = ::sign (n);
@@ -112,18 +119,18 @@ Rational::mod_rat (Rational div) const
 /*
   copy & paste from scm_gcd (GUILE).
  */
-static int
-gcd (long u, long v) 
+static I64
+gcd (I64 u, I64 v)
 {
-  long result = 0;
+  I64 result = 0;
   if (u == 0)
     result = v;
   else if (v == 0)
     result = u;
   else
     {
-      long k = 1;
-      long t;
+      I64 k = 1;
+      I64 t;
       /* Determine a common factor 2^k */
       while (!(1 & (u | v)))
        {
@@ -176,7 +183,7 @@ Rational::normalize ()
     }
   else
     {
-      int g = gcd (num_, den_);
+      I64 g = gcd (num_, den_);
 
       num_ /= g;
       den_ /= g;
@@ -199,7 +206,7 @@ Rational::compare (Rational const &r, Rational const &s)
     return 0;
   else if (r.sign_ == 0)
     return 0;
-  return r.sign_ * ::sign (int (r.num_ * s.den_) - int (s.num_ * r.den_));
+  return r.sign_ * ::sign ((I64) (r.num_ * s.den_) - (I64) (s.num_ * r.den_));
 }
 
 int
@@ -224,9 +231,9 @@ Rational::operator += (Rational r)
     *this = r;
   else
     {
-      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;
+      I64 lcm = (den_ / gcd (r.den_, den_)) * r.den_;
+      I64 n = sign_ * num_ * (lcm / den_) + r.sign_ * r.num_ * (lcm / r.den_);
+      I64 d = lcm;
       sign_ = ::sign (n) * ::sign (d);
       num_ = ::abs (n);
       den_ = ::abs (d);
@@ -258,8 +265,8 @@ Rational::Rational (double x)
        easily.
       */
 
-      num_ = (unsigned int) (mantissa * FACT);
-      den_ = (unsigned int) FACT;
+      num_ = (U64) (mantissa * FACT);
+      den_ = (U64) FACT;
       normalize ();
       if (expt < 0)
        den_ <<= -expt;
@@ -279,7 +286,7 @@ Rational::Rational (double x)
 void
 Rational::invert ()
 {
-  int r (num_);
+  I64 r (num_);
   num_ = den_;
   den_ = r;
 }
index 5232574366c79c950c4e99101b09e5c88535764d..92946a63f8aa950d3b2fa1f8f6239c1a72dbc380 100644 (file)
@@ -57,6 +57,12 @@ to_string (unsigned u)
   return String_convert::unsigned_string (u);
 }
 
+string
+to_string (I64 b, char const *format)
+{
+  return String_convert::i64_string (b, format);
+}
+
 string
 to_string (char const *format, ...)
 {
index 9f62fbb670096940e457dda8c8c79bfda6544e61..9c88983e4c88586d90fc52f643feacaefd6af3c4 100644 (file)
@@ -561,15 +561,16 @@ robust_scm2int (SCM k, int o)
 SCM
 ly_rational2scm (Rational r)
 {
-  return scm_divide (scm_from_int (r.numerator ()), scm_from_int (r.denominator ()));
+  return scm_divide (scm_from_long_long (r.numerator ()),
+                    scm_from_long_long (r.denominator ()));
 }
 
 
 Rational
 ly_scm2rational (SCM r)
 {
-  return Rational (scm_to_int (scm_numerator (r)),
-                  scm_to_int (scm_denominator (r)));
+  return Rational (scm_to_long_long (scm_numerator (r)),
+                  scm_to_long_long (scm_denominator (r)));
 }
 
 Rational