]> www.ginac.de Git - ginac.git/commitdiff
- revamped output of add again to make it use the precedence-rules, which
authorRichard Kreckel <Richard.Kreckel@uni-mainz.de>
Thu, 18 Nov 1999 20:54:30 +0000 (20:54 +0000)
committerRichard Kreckel <Richard.Kreckel@uni-mainz.de>
Thu, 18 Nov 1999 20:54:30 +0000 (20:54 +0000)
  should result in the same output but is more robust and elegant.

ginac/numeric.cpp
ginac/print.cpp

index cc4faf3027d890e4e5876f272cd507ffbc9541e3..165ce594c106e6fd66d69415202c9fb7339c9673 100644 (file)
@@ -519,7 +519,7 @@ numeric const & numeric::operator=(char const * s)
  *  csgn(x)==0 for x==0, csgn(x)==1 for Re(x)>0 or Re(x)=0 and Im(x)>0,
  *  csgn(x)==-1 for Re(x)<0 or Re(x)=0 and Im(x)<0.
  *
  *  csgn(x)==0 for x==0, csgn(x)==1 for Re(x)>0 or Re(x)=0 and Im(x)>0,
  *  csgn(x)==-1 for Re(x)<0 or Re(x)=0 and Im(x)<0.
  *
- *  @see numeric::compare(numeric const @ other) */
+ *  @see numeric::compare(numeric const & other) */
 int numeric::csgn(void) const
 {
     if (is_zero())
 int numeric::csgn(void) const
 {
     if (is_zero())
index 8f70910d45a9143f02504f39d6800ca3cd3d2c6d..f797dc6b1e9ebd59f1c4e525fa27264c64366709 100644 (file)
@@ -84,15 +84,11 @@ void constant::print(ostream & os, unsigned upper_precedence) const
 void power::print(ostream & os, unsigned upper_precedence) const
 {
     debugmsg("power print",LOGLEVEL_PRINT);
 void power::print(ostream & os, unsigned upper_precedence) const
 {
     debugmsg("power print",LOGLEVEL_PRINT);
-#if 1
     if (precedence<=upper_precedence) os << "(";
     basis.print(os,precedence);
     os << "^";
     exponent.print(os,precedence);
     if (precedence<=upper_precedence) os << ")";
     if (precedence<=upper_precedence) os << "(";
     basis.print(os,precedence);
     os << "^";
     exponent.print(os,precedence);
     if (precedence<=upper_precedence) os << ")";
-#else
-    os << "pow(" << basis << "," << exponent << ")";
-#endif
 }
 
 void fail::print(ostream & os, unsigned upper_precedence) const
 }
 
 void fail::print(ostream & os, unsigned upper_precedence) const
@@ -110,49 +106,6 @@ void expairseq::printpair(ostream & os, expair const & p, unsigned upper_precede
     os << "]]";
 }
 
     os << "]]";
 }
 
-/*
-void expairseq::printseq(ostream & os, char delim, unsigned this_precedence,
-                         unsigned upper_precedence) const
-{
-    if (this_precedence<=upper_precedence) os << "(";
-    epvector::const_iterator itt,it,it_last,it_start;
-    it_last=seq.end();
-    --it_last;
-    it_start=seq.begin();
-
-    switch (delim) {
-    case '+':
-        for (it=seq.begin(); it!=it_last; ++it) {
-            itt = it +1;
-            expair const & k = *itt;
-            printpair(os,*it, this_precedence);
-            if (((is_ex_of_type(k.rest, numeric)) && (k.coeff*k.rest > 0) ) || ((!is_ex_of_type(k.rest, numeric)) && (k.coeff >0))){
-                os << "+";
-            }
-        }
-        printpair(os,*it,this_precedence);
-        break;
-        
-    case '*':
-        for (it = it_last ; it!=it_start; --it) {
-            if ((*it).rest.is_equal(exMINUSONE()) &&
-                (*it).coeff.is_equal(exONE())) {
-                os << "-";
-            } else {
-                printpair(os, *it,this_precedence);
-                os << delim;
-            }
-        }
-        printpair(os,*it,this_precedence);
-        break;
-    default:
-        clog << "Nobody expects the Spanish Inquisition: "
-             << "deliminator unknown!" << endl;
-    }
-    if (this_precedence<=upper_precedence) os << ")";
-}
-*/
-
 void expairseq::printseq(ostream & os, char delim, unsigned this_precedence,
                          unsigned upper_precedence) const
 {
 void expairseq::printseq(ostream & os, char delim, unsigned this_precedence,
                          unsigned upper_precedence) const
 {
@@ -188,18 +141,16 @@ void add::print(ostream & os, unsigned upper_precedence) const
     for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
         coeff = ex_to_numeric(cit->coeff);
         if (!first) {
     for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
         coeff = ex_to_numeric(cit->coeff);
         if (!first) {
-            if (coeff < 0) os << '-'; else os << '+';
+            if (coeff.csgn()==-1) os << '-'; else os << '+';
         } else {
         } else {
-            if (coeff < 0) os << '-';
+            if (coeff.csgn()==-1) os << '-';
             first=false;
         }
         if (coeff.compare(numONE()) && coeff.compare(numMINUSONE())) {
             first=false;
         }
         if (coeff.compare(numONE()) && coeff.compare(numMINUSONE())) {
-            if (!coeff.is_real() && !coeff.real().is_zero()) os << '(';
-            if (coeff > 0)
-                os << coeff;
+            if (coeff.csgn()==-1)
+                (numMINUSONE()*coeff).print(os, precedence);
             else
             else
-                os << numeric(-1)*coeff;
-            if (!coeff.is_real() && !coeff.real().is_zero()) os << ')';
+                coeff.print(os, precedence);
             os << '*';
         }
         os << cit->rest;
             os << '*';
         }
         os << cit->rest;