]> www.ginac.de Git - ginac.git/blobdiff - ginac/numeric.cpp
Fixed warning on 64bit machines.
[ginac.git] / ginac / numeric.cpp
index 8825bdb992f6a78825fef3b9ace567a3b0e64591..582bfdcfb5be19229c56e7239217c3a3c0ff93b0 100644 (file)
@@ -7,7 +7,7 @@
  *  of special functions or implement the interface to the bignum package. */
 
 /*
- *  GiNaC Copyright (C) 1999-2005 Johannes Gutenberg University Mainz, Germany
+ *  GiNaC Copyright (C) 1999-2006 Johannes Gutenberg University Mainz, Germany
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -73,7 +73,7 @@ GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(numeric, basic,
 //////////
 
 /** default ctor. Numerically it initializes to an integer zero. */
-numeric::numeric() : basic(TINFO_numeric)
+numeric::numeric() : basic(&numeric::tinfo_static)
 {
        value = cln::cl_I(0);
        setflag(status_flags::evaluated | status_flags::expanded);
@@ -85,44 +85,56 @@ numeric::numeric() : basic(TINFO_numeric)
 
 // public
 
-numeric::numeric(int i) : basic(TINFO_numeric)
+numeric::numeric(int i) : basic(&numeric::tinfo_static)
 {
        // Not the whole int-range is available if we don't cast to long
        // first.  This is due to the behaviour of the cl_I-ctor, which
        // emphasizes efficiency.  However, if the integer is small enough
        // we save space and dereferences by using an immediate type.
        // (C.f. <cln/object.h>)
+       // The #if clause prevents compiler warnings on 64bit machines where the
+       // comparision is always true.
+#if cl_value_len >= 32
+       value = cln::cl_I(i);
+#else
        if (i < (1L << (cl_value_len-1)) && i >= -(1L << (cl_value_len-1)))
                value = cln::cl_I(i);
        else
                value = cln::cl_I(static_cast<long>(i));
+#endif
        setflag(status_flags::evaluated | status_flags::expanded);
 }
 
 
-numeric::numeric(unsigned int i) : basic(TINFO_numeric)
+numeric::numeric(unsigned int i) : basic(&numeric::tinfo_static)
 {
        // Not the whole uint-range is available if we don't cast to ulong
        // first.  This is due to the behaviour of the cl_I-ctor, which
        // emphasizes efficiency.  However, if the integer is small enough
        // we save space and dereferences by using an immediate type.
        // (C.f. <cln/object.h>)
-       if (i < (1U << (cl_value_len-1)))
+       // The #if clause prevents compiler warnings on 64bit machines where the
+       // comparision is always true.
+#if cl_value_len >= 32
+       value = cln::cl_I(i);
+#else
+       if (i < (1UL << (cl_value_len-1)))
                value = cln::cl_I(i);
        else
                value = cln::cl_I(static_cast<unsigned long>(i));
+#endif
        setflag(status_flags::evaluated | status_flags::expanded);
 }
 
 
-numeric::numeric(long i) : basic(TINFO_numeric)
+numeric::numeric(long i) : basic(&numeric::tinfo_static)
 {
        value = cln::cl_I(i);
        setflag(status_flags::evaluated | status_flags::expanded);
 }
 
 
-numeric::numeric(unsigned long i) : basic(TINFO_numeric)
+numeric::numeric(unsigned long i) : basic(&numeric::tinfo_static)
 {
        value = cln::cl_I(i);
        setflag(status_flags::evaluated | status_flags::expanded);
@@ -132,7 +144,7 @@ numeric::numeric(unsigned long i) : basic(TINFO_numeric)
 /** Constructor for rational numerics a/b.
  *
  *  @exception overflow_error (division by zero) */
-numeric::numeric(long numer, long denom) : basic(TINFO_numeric)
+numeric::numeric(long numer, long denom) : basic(&numeric::tinfo_static)
 {
        if (!denom)
                throw std::overflow_error("division by zero");
@@ -141,7 +153,7 @@ numeric::numeric(long numer, long denom) : basic(TINFO_numeric)
 }
 
 
-numeric::numeric(double d) : basic(TINFO_numeric)
+numeric::numeric(double d) : basic(&numeric::tinfo_static)
 {
        // We really want to explicitly use the type cl_LF instead of the
        // more general cl_F, since that would give us a cl_DF only which
@@ -153,7 +165,7 @@ numeric::numeric(double d) : basic(TINFO_numeric)
 
 /** ctor from C-style string.  It also accepts complex numbers in GiNaC
  *  notation like "2+5*I". */
-numeric::numeric(const char *s) : basic(TINFO_numeric)
+numeric::numeric(const char *s) : basic(&numeric::tinfo_static)
 {
        cln::cl_N ctorval = 0;
        // parse complex numbers (functional but not completely safe, unfortunately
@@ -232,7 +244,7 @@ numeric::numeric(const char *s) : basic(TINFO_numeric)
 
 /** Ctor from CLN types.  This is for the initiated user or internal use
  *  only. */
-numeric::numeric(const cln::cl_N &z) : basic(TINFO_numeric)
+numeric::numeric(const cln::cl_N &z) : basic(&numeric::tinfo_static)
 {
        value = z;
        setflag(status_flags::evaluated | status_flags::expanded);
@@ -603,6 +615,11 @@ bool numeric::info(unsigned inf) const
        return false;
 }
 
+bool numeric::is_polynomial(const ex & var) const
+{
+       return true;
+}
+
 int numeric::degree(const ex & s) const
 {
        return 0;
@@ -624,7 +641,7 @@ ex numeric::coeff(const ex & s, int n) const
  *  results:  (2+I).has(-2) -> true.  But this is consistent, since we also
  *  would like to have (-2+I).has(2) -> true and we want to think about the
  *  sign as a multiplicative factor. */
-bool numeric::has(const ex &other) const
+bool numeric::has(const ex &other, unsigned options) const
 {
        if (!is_exactly_a<numeric>(other))
                return false;
@@ -682,6 +699,16 @@ ex numeric::conjugate() const
        return numeric(cln::conjugate(this->value));
 }
 
+ex numeric::real_part() const
+{
+       return numeric(cln::realpart(value));
+}
+
+ex numeric::imag_part() const
+{
+       return numeric(cln::imagpart(value));
+}
+
 // protected
 
 int numeric::compare_same_type(const basic &other) const
@@ -927,6 +954,18 @@ const numeric numeric::inverse() const
        return numeric(cln::recip(value));
 }
 
+/** Return the step function of a numeric. The imaginary part of it is
+ *  ignored because the step function is generally considered real but
+ *  a numeric may develop a small imaginary part due to rounding errors.
+ */
+numeric numeric::step() const
+{      cln::cl_R r = cln::realpart(value);
+       if(cln::zerop(r))
+               return numeric(1,2);
+       if(cln::plusp(r))
+               return 1;
+       return 0;
+}
 
 /** Return the complex half-plane (left or right) in which the number lies.
  *  csgn(x)==0 for x==0, csgn(x)==1 for Re(x)>0 or Re(x)=0 and Im(x)>0,
@@ -1709,19 +1748,19 @@ const numeric bernoulli(const numeric &nn)
        for (unsigned p=next_r; p<=n;  p+=2) {
                cln::cl_I  c = 1;  // seed for binonmial coefficients
                cln::cl_RA b = cln::cl_RA(p-1)/-2;
-               const unsigned p3 = p+3;
-               const unsigned pm = p-2;
-               unsigned i, k, p_2;
-               // test if intermediate unsigned int can be represented by immediate
-               // objects by CLN (i.e. < 2^29 for 32 Bit machines, see <cln/object.h>)
+               // The CLN manual says: "The conversion from `unsigned int' works only
+               // if the argument is < 2^29" (This is for 32 Bit machines. More
+               // generally, cl_value_len is the limiting exponent of 2. We must make
+               // sure that no intermediates are created which exceed this value. The
+               // largest intermediate is (p+3-2*k)*(p/2-k+1) <= (p^2+p)/2.
                if (p < (1UL<<cl_value_len/2)) {
-                       for (i=2, k=1, p_2=p/2; i<=pm; i+=2, ++k, --p_2) {
-                               c = cln::exquo(c * ((p3-i) * p_2), (i-1)*k);
+                       for (unsigned k=1; k<=p/2-1; ++k) {
+                               c = cln::exquo(c * ((p+3-2*k) * (p/2-k+1)), (2*k-1)*k);
                                b = b + c*results[k-1];
                        }
                } else {
-                       for (i=2, k=1, p_2=p/2; i<=pm; i+=2, ++k, --p_2) {
-                               c = cln::exquo((c * (p3-i)) * p_2, cln::cl_I(i-1)*k);
+                       for (unsigned k=1; k<=p/2-1; ++k) {
+                               c = cln::exquo((c * (p+3-2*k)) * (p/2-k+1), cln::cl_I(2*k-1)*k);
                                b = b + c*results[k-1];
                        }
                }