]> www.ginac.de Git - ginac.git/blob - ginac/numeric.cpp
- doublefactorial now falls back directly to CLN, which is much faster.
[ginac.git] / ginac / numeric.cpp
1 /** @file numeric.cpp
2  *
3  *  This file contains the interface to the underlying bignum package.
4  *  Its most important design principle is to completely hide the inner
5  *  working of that other package from the user of GiNaC.  It must either 
6  *  provide implementation of arithmetic operators and numerical evaluation
7  *  of special functions or implement the interface to the bignum package. */
8
9 /*
10  *  GiNaC Copyright (C) 1999-2000 Johannes Gutenberg University Mainz, Germany
11  *
12  *  This program is free software; you can redistribute it and/or modify
13  *  it under the terms of the GNU General Public License as published by
14  *  the Free Software Foundation; either version 2 of the License, or
15  *  (at your option) any later version.
16  *
17  *  This program is distributed in the hope that it will be useful,
18  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *  GNU General Public License for more details.
21  *
22  *  You should have received a copy of the GNU General Public License
23  *  along with this program; if not, write to the Free Software
24  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25  */
26
27 #include <vector>
28 #include <stdexcept>
29
30 #include "numeric.h"
31 #include "ex.h"
32 #include "config.h"
33 #include "debugmsg.h"
34 #include "utils.h"
35
36 // CLN should not pollute the global namespace, hence we include it here
37 // instead of in some header file where it would propagate to other parts:
38 #ifdef HAVE_CLN_CLN_H
39 #include <CLN/cln.h>
40 #else
41 #include <cln.h>
42 #endif
43
44 #ifndef NO_GINAC_NAMESPACE
45 namespace GiNaC {
46 #endif // ndef NO_GINAC_NAMESPACE
47
48 // linker has no problems finding text symbols for numerator or denominator
49 //#define SANE_LINKER
50
51 //////////
52 // default constructor, destructor, copy constructor assignment
53 // operator and helpers
54 //////////
55
56 // public
57
58 /** default ctor. Numerically it initializes to an integer zero. */
59 numeric::numeric() : basic(TINFO_numeric)
60 {
61     debugmsg("numeric default constructor", LOGLEVEL_CONSTRUCT);
62     value = new cl_N;
63     *value=cl_I(0);
64     calchash();
65     setflag(status_flags::evaluated|
66             status_flags::hash_calculated);
67 }
68
69 numeric::~numeric()
70 {
71     debugmsg("numeric destructor" ,LOGLEVEL_DESTRUCT);
72     destroy(0);
73 }
74
75 numeric::numeric(const numeric & other)
76 {
77     debugmsg("numeric copy constructor", LOGLEVEL_CONSTRUCT);
78     copy(other);
79 }
80
81 const numeric & numeric::operator=(const numeric & other)
82 {
83     debugmsg("numeric operator=", LOGLEVEL_ASSIGNMENT);
84     if (this != &other) {
85         destroy(1);
86         copy(other);
87     }
88     return *this;
89 }
90
91 // protected
92
93 void numeric::copy(const numeric & other)
94 {
95     basic::copy(other);
96     value = new cl_N(*other.value);
97 }
98
99 void numeric::destroy(bool call_parent)
100 {
101     delete value;
102     if (call_parent) basic::destroy(call_parent);
103 }
104
105 //////////
106 // other constructors
107 //////////
108
109 // public
110
111 numeric::numeric(int i) : basic(TINFO_numeric)
112 {
113     debugmsg("const numericructor from int",LOGLEVEL_CONSTRUCT);
114     // Not the whole int-range is available if we don't cast to long
115     // first. This is due to the behaviour of the cl_I-ctor, which
116     // emphasizes efficiency:
117     value = new cl_I((long) i);
118     calchash();
119     setflag(status_flags::evaluated|
120             status_flags::hash_calculated);
121 }
122
123 numeric::numeric(unsigned int i) : basic(TINFO_numeric)
124 {
125     debugmsg("const numericructor from uint",LOGLEVEL_CONSTRUCT);
126     // Not the whole uint-range is available if we don't cast to ulong
127     // first. This is due to the behaviour of the cl_I-ctor, which
128     // emphasizes efficiency:
129     value = new cl_I((unsigned long)i);
130     calchash();
131     setflag(status_flags::evaluated|
132             status_flags::hash_calculated);
133 }
134
135 numeric::numeric(long i) : basic(TINFO_numeric)
136 {
137     debugmsg("const numericructor from long",LOGLEVEL_CONSTRUCT);
138     value = new cl_I(i);
139     calchash();
140     setflag(status_flags::evaluated|
141             status_flags::hash_calculated);
142 }
143
144 numeric::numeric(unsigned long i) : basic(TINFO_numeric)
145 {
146     debugmsg("const numericructor from ulong",LOGLEVEL_CONSTRUCT);
147     value = new cl_I(i);
148     calchash();
149     setflag(status_flags::evaluated|
150             status_flags::hash_calculated);
151 }
152
153 /** Ctor for rational numerics a/b.
154  *
155  *  @exception overflow_error (division by zero) */
156 numeric::numeric(long numer, long denom) : basic(TINFO_numeric)
157 {
158     debugmsg("const numericructor from long/long",LOGLEVEL_CONSTRUCT);
159     if (!denom)
160         throw (std::overflow_error("division by zero"));
161     value = new cl_I(numer);
162     *value = *value / cl_I(denom);
163     calchash();
164     setflag(status_flags::evaluated|
165             status_flags::hash_calculated);
166 }
167
168 numeric::numeric(double d) : basic(TINFO_numeric)
169 {
170     debugmsg("const numericructor from double",LOGLEVEL_CONSTRUCT);
171     // We really want to explicitly use the type cl_LF instead of the
172     // more general cl_F, since that would give us a cl_DF only which
173     // will not be promoted to cl_LF if overflow occurs:
174     value = new cl_N;
175     *value = cl_float(d, cl_default_float_format);
176     calchash();
177     setflag(status_flags::evaluated|
178             status_flags::hash_calculated);
179 }
180
181 numeric::numeric(char const *s) : basic(TINFO_numeric)
182 {   // MISSING: treatment of complex and ints and rationals.
183     debugmsg("const numericructor from string",LOGLEVEL_CONSTRUCT);
184     if (strchr(s, '.'))
185         value = new cl_LF(s);
186     else
187         value = new cl_I(s);
188     calchash();
189     setflag(status_flags::evaluated|
190             status_flags::hash_calculated);
191 }
192
193 /** Ctor from CLN types.  This is for the initiated user or internal use
194  *  only. */
195 numeric::numeric(cl_N const & z) : basic(TINFO_numeric)
196 {
197     debugmsg("const numericructor from cl_N", LOGLEVEL_CONSTRUCT);
198     value = new cl_N(z);
199     calchash();
200     setflag(status_flags::evaluated|
201             status_flags::hash_calculated);
202 }
203
204 //////////
205 // functions overriding virtual functions from bases classes
206 //////////
207
208 // public
209
210 basic * numeric::duplicate() const
211 {
212     debugmsg("numeric duplicate", LOGLEVEL_DUPLICATE);
213     return new numeric(*this);
214 }
215
216 void numeric::print(ostream & os, unsigned upper_precedence) const
217 {
218     // The method print adds to the output so it blends more consistently
219     // together with the other routines and produces something compatible to
220     // ginsh input.
221     debugmsg("numeric print", LOGLEVEL_PRINT);
222     if (is_real()) {
223         // case 1, real:  x  or  -x
224         if ((precedence<=upper_precedence) && (!is_pos_integer())) {
225             os << "(" << *value << ")";
226         } else {
227             os << *value;
228         }
229     } else {
230         // case 2, imaginary:  y*I  or  -y*I
231         if (realpart(*value) == 0) {
232             if ((precedence<=upper_precedence) && (imagpart(*value) < 0)) {
233                 if (imagpart(*value) == -1) {
234                     os << "(-I)";
235                 } else {
236                     os << "(" << imagpart(*value) << "*I)";
237                 }
238             } else {
239                 if (imagpart(*value) == 1) {
240                     os << "I";
241                 } else {
242                     if (imagpart (*value) == -1) {
243                         os << "-I";
244                     } else {
245                         os << imagpart(*value) << "*I";
246                     }
247                 }
248             }
249         } else {
250             // case 3, complex:  x+y*I  or  x-y*I  or  -x+y*I  or  -x-y*I
251             if (precedence <= upper_precedence) os << "(";
252             os << realpart(*value);
253             if (imagpart(*value) < 0) {
254                 if (imagpart(*value) == -1) {
255                     os << "-I";
256                 } else {
257                     os << imagpart(*value) << "*I";
258                 }
259             } else {
260                 if (imagpart(*value) == 1) {
261                     os << "+I";
262                 } else {
263                     os << "+" << imagpart(*value) << "*I";
264                 }
265             }
266             if (precedence <= upper_precedence) os << ")";
267         }
268     }
269 }
270
271
272 void numeric::printraw(ostream & os) const
273 {
274     // The method printraw doesn't do much, it simply uses CLN's operator<<()
275     // for output, which is ugly but reliable. e.g: 2+2i
276     debugmsg("numeric printraw", LOGLEVEL_PRINT);
277     os << "numeric(" << *value << ")";
278 }
279 void numeric::printtree(ostream & os, unsigned indent) const
280 {
281     debugmsg("numeric printtree", LOGLEVEL_PRINT);
282     os << string(indent,' ') << *value
283        << " (numeric): "
284        << "hash=" << hashvalue << " (0x" << hex << hashvalue << dec << ")"
285        << ", flags=" << flags << endl;
286 }
287
288 void numeric::printcsrc(ostream & os, unsigned type, unsigned upper_precedence) const
289 {
290     debugmsg("numeric print csrc", LOGLEVEL_PRINT);
291     ios::fmtflags oldflags = os.flags();
292     os.setf(ios::scientific);
293     if (is_rational() && !is_integer()) {
294         if (compare(_num0()) > 0) {
295             os << "(";
296             if (type == csrc_types::ctype_cl_N)
297                 os << "cl_F(\"" << numer().evalf() << "\")";
298             else
299                 os << numer().to_double();
300         } else {
301             os << "-(";
302             if (type == csrc_types::ctype_cl_N)
303                 os << "cl_F(\"" << -numer().evalf() << "\")";
304             else
305                 os << -numer().to_double();
306         }
307         os << "/";
308         if (type == csrc_types::ctype_cl_N)
309             os << "cl_F(\"" << denom().evalf() << "\")";
310         else
311             os << denom().to_double();
312         os << ")";
313     } else {
314         if (type == csrc_types::ctype_cl_N)
315             os << "cl_F(\"" << evalf() << "\")";
316         else
317             os << to_double();
318     }
319     os.flags(oldflags);
320 }
321
322 bool numeric::info(unsigned inf) const
323 {
324     switch (inf) {
325     case info_flags::numeric:
326     case info_flags::polynomial:
327     case info_flags::rational_function:
328         return true;
329     case info_flags::real:
330         return is_real();
331     case info_flags::rational:
332     case info_flags::rational_polynomial:
333         return is_rational();
334     case info_flags::crational:
335     case info_flags::crational_polynomial:
336         return is_crational();
337     case info_flags::integer:
338     case info_flags::integer_polynomial:
339         return is_integer();
340     case info_flags::cinteger:
341     case info_flags::cinteger_polynomial:
342         return is_cinteger();
343     case info_flags::positive:
344         return is_positive();
345     case info_flags::negative:
346         return is_negative();
347     case info_flags::nonnegative:
348         return compare(_num0())>=0;
349     case info_flags::posint:
350         return is_pos_integer();
351     case info_flags::negint:
352         return is_integer() && (compare(_num0())<0);
353     case info_flags::nonnegint:
354         return is_nonneg_integer();
355     case info_flags::even:
356         return is_even();
357     case info_flags::odd:
358         return is_odd();
359     case info_flags::prime:
360         return is_prime();
361     }
362     return false;
363 }
364
365 /** Cast numeric into a floating-point object.  For example exact numeric(1) is
366  *  returned as a 1.0000000000000000000000 and so on according to how Digits is
367  *  currently set.
368  *
369  *  @param level  ignored, but needed for overriding basic::evalf.
370  *  @return  an ex-handle to a numeric. */
371 ex numeric::evalf(int level) const
372 {
373     // level can safely be discarded for numeric objects.
374     return numeric(cl_float(1.0, cl_default_float_format) * (*value));  // -> CLN
375 }
376
377 // protected
378
379 int numeric::compare_same_type(basic const & other) const
380 {
381     GINAC_ASSERT(is_exactly_of_type(other, numeric));
382     const numeric & o = static_cast<numeric &>(const_cast<basic &>(other));
383
384     if (*value == *o.value) {
385         return 0;
386     }
387
388     return compare(o);    
389 }
390
391 bool numeric::is_equal_same_type(basic const & other) const
392 {
393     GINAC_ASSERT(is_exactly_of_type(other,numeric));
394     const numeric *o = static_cast<const numeric *>(&other);
395     
396     return is_equal(*o);
397 }
398
399 /*
400 unsigned numeric::calchash(void) const
401 {
402     double d=to_double();
403     int s=d>0 ? 1 : -1;
404     d=fabs(d);
405     if (d>0x07FF0000) {
406         d=0x07FF0000;
407     }
408     return 0x88000000U+s*unsigned(d/0x07FF0000);
409 }
410 */
411
412
413 //////////
414 // new virtual functions which can be overridden by derived classes
415 //////////
416
417 // none
418
419 //////////
420 // non-virtual functions in this class
421 //////////
422
423 // public
424
425 /** Numerical addition method.  Adds argument to *this and returns result as
426  *  a new numeric object. */
427 numeric numeric::add(const numeric & other) const
428 {
429     return numeric((*value)+(*other.value));
430 }
431
432 /** Numerical subtraction method.  Subtracts argument from *this and returns
433  *  result as a new numeric object. */
434 numeric numeric::sub(const numeric & other) const
435 {
436     return numeric((*value)-(*other.value));
437 }
438
439 /** Numerical multiplication method.  Multiplies *this and argument and returns
440  *  result as a new numeric object. */
441 numeric numeric::mul(const numeric & other) const
442 {
443     static const numeric * _num1p=&_num1();
444     if (this==_num1p) {
445         return other;
446     } else if (&other==_num1p) {
447         return *this;
448     }
449     return numeric((*value)*(*other.value));
450 }
451
452 /** Numerical division method.  Divides *this by argument and returns result as
453  *  a new numeric object.
454  *
455  *  @exception overflow_error (division by zero) */
456 numeric numeric::div(const numeric & other) const
457 {
458     if (::zerop(*other.value))
459         throw (std::overflow_error("division by zero"));
460     return numeric((*value)/(*other.value));
461 }
462
463 numeric numeric::power(const numeric & other) const
464 {
465     static const numeric * _num1p=&_num1();
466     if (&other==_num1p)
467         return *this;
468     if (::zerop(*value) && other.is_real() && ::minusp(realpart(*other.value)))
469         throw (std::overflow_error("division by zero"));
470     return numeric(::expt(*value,*other.value));
471 }
472
473 /** Inverse of a number. */
474 numeric numeric::inverse(void) const
475 {
476     return numeric(::recip(*value));  // -> CLN
477 }
478
479 const numeric & numeric::add_dyn(const numeric & other) const
480 {
481     return static_cast<const numeric &>((new numeric((*value)+(*other.value)))->
482                                         setflag(status_flags::dynallocated));
483 }
484
485 const numeric & numeric::sub_dyn(const numeric & other) const
486 {
487     return static_cast<const numeric &>((new numeric((*value)-(*other.value)))->
488                                         setflag(status_flags::dynallocated));
489 }
490
491 const numeric & numeric::mul_dyn(const numeric & other) const
492 {
493     static const numeric * _num1p=&_num1();
494     if (this==_num1p) {
495         return other;
496     } else if (&other==_num1p) {
497         return *this;
498     }
499     return static_cast<const numeric &>((new numeric((*value)*(*other.value)))->
500                                         setflag(status_flags::dynallocated));
501 }
502
503 const numeric & numeric::div_dyn(const numeric & other) const
504 {
505     if (::zerop(*other.value))
506         throw (std::overflow_error("division by zero"));
507     return static_cast<const numeric &>((new numeric((*value)/(*other.value)))->
508                                         setflag(status_flags::dynallocated));
509 }
510
511 const numeric & numeric::power_dyn(const numeric & other) const
512 {
513     static const numeric * _num1p=&_num1();
514     if (&other==_num1p)
515         return *this;
516     if (::zerop(*value) && other.is_real() && ::minusp(realpart(*other.value)))
517         throw (std::overflow_error("division by zero"));
518     return static_cast<const numeric &>((new numeric(::expt(*value,*other.value)))->
519                                         setflag(status_flags::dynallocated));
520 }
521
522 const numeric & numeric::operator=(int i)
523 {
524     return operator=(numeric(i));
525 }
526
527 const numeric & numeric::operator=(unsigned int i)
528 {
529     return operator=(numeric(i));
530 }
531
532 const numeric & numeric::operator=(long i)
533 {
534     return operator=(numeric(i));
535 }
536
537 const numeric & numeric::operator=(unsigned long i)
538 {
539     return operator=(numeric(i));
540 }
541
542 const numeric & numeric::operator=(double d)
543 {
544     return operator=(numeric(d));
545 }
546
547 const numeric & numeric::operator=(char const * s)
548 {
549     return operator=(numeric(s));
550 }
551
552 /** Return the complex half-plane (left or right) in which the number lies.
553  *  csgn(x)==0 for x==0, csgn(x)==1 for Re(x)>0 or Re(x)=0 and Im(x)>0,
554  *  csgn(x)==-1 for Re(x)<0 or Re(x)=0 and Im(x)<0.
555  *
556  *  @see numeric::compare(const numeric & other) */
557 int numeric::csgn(void) const
558 {
559     if (is_zero())
560         return 0;
561     if (!::zerop(realpart(*value))) {
562         if (::plusp(realpart(*value)))
563             return 1;
564         else
565             return -1;
566     } else {
567         if (::plusp(imagpart(*value)))
568             return 1;
569         else
570             return -1;
571     }
572 }
573
574 /** This method establishes a canonical order on all numbers.  For complex
575  *  numbers this is not possible in a mathematically consistent way but we need
576  *  to establish some order and it ought to be fast.  So we simply define it
577  *  to be compatible with our method csgn.
578  *
579  *  @return csgn(*this-other)
580  *  @see numeric::csgn(void) */
581 int numeric::compare(const numeric & other) const
582 {
583     // Comparing two real numbers?
584     if (is_real() && other.is_real())
585         // Yes, just compare them
586         return ::cl_compare(The(cl_R)(*value), The(cl_R)(*other.value));    
587     else {
588         // No, first compare real parts
589         cl_signean real_cmp = ::cl_compare(realpart(*value), realpart(*other.value));
590         if (real_cmp)
591             return real_cmp;
592
593         return ::cl_compare(imagpart(*value), imagpart(*other.value));
594     }
595 }
596
597 bool numeric::is_equal(const numeric & other) const
598 {
599     return (*value == *other.value);
600 }
601
602 /** True if object is zero. */
603 bool numeric::is_zero(void) const
604 {
605     return ::zerop(*value);  // -> CLN
606 }
607
608 /** True if object is not complex and greater than zero. */
609 bool numeric::is_positive(void) const
610 {
611     if (is_real())
612         return ::plusp(The(cl_R)(*value));  // -> CLN
613     return false;
614 }
615
616 /** True if object is not complex and less than zero. */
617 bool numeric::is_negative(void) const
618 {
619     if (is_real())
620         return ::minusp(The(cl_R)(*value));  // -> CLN
621     return false;
622 }
623
624 /** True if object is a non-complex integer. */
625 bool numeric::is_integer(void) const
626 {
627     return ::instanceof(*value, cl_I_ring);  // -> CLN
628 }
629
630 /** True if object is an exact integer greater than zero. */
631 bool numeric::is_pos_integer(void) const
632 {
633     return (is_integer() && ::plusp(The(cl_I)(*value)));  // -> CLN
634 }
635
636 /** True if object is an exact integer greater or equal zero. */
637 bool numeric::is_nonneg_integer(void) const
638 {
639     return (is_integer() && !::minusp(The(cl_I)(*value)));  // -> CLN
640 }
641
642 /** True if object is an exact even integer. */
643 bool numeric::is_even(void) const
644 {
645     return (is_integer() && ::evenp(The(cl_I)(*value)));  // -> CLN
646 }
647
648 /** True if object is an exact odd integer. */
649 bool numeric::is_odd(void) const
650 {
651     return (is_integer() && ::oddp(The(cl_I)(*value)));  // -> CLN
652 }
653
654 /** Probabilistic primality test.
655  *
656  *  @return  true if object is exact integer and prime. */
657 bool numeric::is_prime(void) const
658 {
659     return (is_integer() && ::isprobprime(The(cl_I)(*value)));  // -> CLN
660 }
661
662 /** True if object is an exact rational number, may even be complex
663  *  (denominator may be unity). */
664 bool numeric::is_rational(void) const
665 {
666     return ::instanceof(*value, cl_RA_ring);  // -> CLN
667 }
668
669 /** True if object is a real integer, rational or float (but not complex). */
670 bool numeric::is_real(void) const
671 {
672     return ::instanceof(*value, cl_R_ring);  // -> CLN
673 }
674
675 bool numeric::operator==(const numeric & other) const
676 {
677     return (*value == *other.value);  // -> CLN
678 }
679
680 bool numeric::operator!=(const numeric & other) const
681 {
682     return (*value != *other.value);  // -> CLN
683 }
684
685 /** True if object is element of the domain of integers extended by I, i.e. is
686  *  of the form a+b*I, where a and b are integers. */
687 bool numeric::is_cinteger(void) const
688 {
689     if (::instanceof(*value, cl_I_ring))
690         return true;
691     else if (!is_real()) {  // complex case, handle n+m*I
692         if (::instanceof(realpart(*value), cl_I_ring) &&
693             ::instanceof(imagpart(*value), cl_I_ring))
694             return true;
695     }
696     return false;
697 }
698
699 /** True if object is an exact rational number, may even be complex
700  *  (denominator may be unity). */
701 bool numeric::is_crational(void) const
702 {
703     if (::instanceof(*value, cl_RA_ring))
704         return true;
705     else if (!is_real()) {  // complex case, handle Q(i):
706         if (::instanceof(realpart(*value), cl_RA_ring) &&
707             ::instanceof(imagpart(*value), cl_RA_ring))
708             return true;
709     }
710     return false;
711 }
712
713 /** Numerical comparison: less.
714  *
715  *  @exception invalid_argument (complex inequality) */ 
716 bool numeric::operator<(const numeric & other) const
717 {
718     if (is_real() && other.is_real())
719         return (bool)(The(cl_R)(*value) < The(cl_R)(*other.value));  // -> CLN
720     throw (std::invalid_argument("numeric::operator<(): complex inequality"));
721     return false;  // make compiler shut up
722 }
723
724 /** Numerical comparison: less or equal.
725  *
726  *  @exception invalid_argument (complex inequality) */ 
727 bool numeric::operator<=(const numeric & other) const
728 {
729     if (is_real() && other.is_real())
730         return (bool)(The(cl_R)(*value) <= The(cl_R)(*other.value));  // -> CLN
731     throw (std::invalid_argument("numeric::operator<=(): complex inequality"));
732     return false;  // make compiler shut up
733 }
734
735 /** Numerical comparison: greater.
736  *
737  *  @exception invalid_argument (complex inequality) */ 
738 bool numeric::operator>(const numeric & other) const
739 {
740     if (is_real() && other.is_real())
741         return (bool)(The(cl_R)(*value) > The(cl_R)(*other.value));  // -> CLN
742     throw (std::invalid_argument("numeric::operator>(): complex inequality"));
743     return false;  // make compiler shut up
744 }
745
746 /** Numerical comparison: greater or equal.
747  *
748  *  @exception invalid_argument (complex inequality) */  
749 bool numeric::operator>=(const numeric & other) const
750 {
751     if (is_real() && other.is_real())
752         return (bool)(The(cl_R)(*value) >= The(cl_R)(*other.value));  // -> CLN
753     throw (std::invalid_argument("numeric::operator>=(): complex inequality"));
754     return false;  // make compiler shut up
755 }
756
757 /** Converts numeric types to machine's int. You should check with is_integer()
758  *  if the number is really an integer before calling this method. */
759 int numeric::to_int(void) const
760 {
761     GINAC_ASSERT(is_integer());
762     return ::cl_I_to_int(The(cl_I)(*value));  // -> CLN
763 }
764
765 /** Converts numeric types to machine's double. You should check with is_real()
766  *  if the number is really not complex before calling this method. */
767 double numeric::to_double(void) const
768 {
769     GINAC_ASSERT(is_real());
770     return ::cl_double_approx(realpart(*value));  // -> CLN
771 }
772
773 /** Real part of a number. */
774 numeric numeric::real(void) const
775 {
776     return numeric(::realpart(*value));  // -> CLN
777 }
778
779 /** Imaginary part of a number. */
780 numeric numeric::imag(void) const
781 {
782     return numeric(::imagpart(*value));  // -> CLN
783 }
784
785 #ifndef SANE_LINKER
786 // Unfortunately, CLN did not provide an official way to access the numerator
787 // or denominator of a rational number (cl_RA). Doing some excavations in CLN
788 // one finds how it works internally in src/rational/cl_RA.h:
789 struct cl_heap_ratio : cl_heap {
790     cl_I numerator;
791     cl_I denominator;
792 };
793
794 inline cl_heap_ratio* TheRatio (const cl_N& obj)
795 { return (cl_heap_ratio*)(obj.pointer); }
796 #endif // ndef SANE_LINKER
797
798 /** Numerator.  Computes the numerator of rational numbers, rationalized
799  *  numerator of complex if real and imaginary part are both rational numbers
800  *  (i.e numer(4/3+5/6*I) == 8+5*I), the number carrying the sign in all other
801  *  cases. */
802 numeric numeric::numer(void) const
803 {
804     if (is_integer()) {
805         return numeric(*this);
806     }
807 #ifdef SANE_LINKER
808     else if (::instanceof(*value, cl_RA_ring)) {
809         return numeric(::numerator(The(cl_RA)(*value)));
810     }
811     else if (!is_real()) {  // complex case, handle Q(i):
812         cl_R r = ::realpart(*value);
813         cl_R i = ::imagpart(*value);
814         if (::instanceof(r, cl_I_ring) && ::instanceof(i, cl_I_ring))
815             return numeric(*this);
816         if (::instanceof(r, cl_I_ring) && ::instanceof(i, cl_RA_ring))
817             return numeric(complex(r*::denominator(The(cl_RA)(i)), ::numerator(The(cl_RA)(i))));
818         if (::instanceof(r, cl_RA_ring) && ::instanceof(i, cl_I_ring))
819             return numeric(complex(::numerator(The(cl_RA)(r)), i*::denominator(The(cl_RA)(r))));
820         if (::instanceof(r, cl_RA_ring) && ::instanceof(i, cl_RA_ring)) {
821             cl_I s = lcm(::denominator(The(cl_RA)(r)), ::denominator(The(cl_RA)(i)));
822             return numeric(complex(::numerator(The(cl_RA)(r))*(exquo(s,::denominator(The(cl_RA)(r)))),
823                                    ::numerator(The(cl_RA)(i))*(exquo(s,::denominator(The(cl_RA)(i))))));
824         }
825     }
826 #else
827     else if (instanceof(*value, cl_RA_ring)) {
828         return numeric(TheRatio(*value)->numerator);
829     }
830     else if (!is_real()) {  // complex case, handle Q(i):
831         cl_R r = realpart(*value);
832         cl_R i = imagpart(*value);
833         if (instanceof(r, cl_I_ring) && instanceof(i, cl_I_ring))
834             return numeric(*this);
835         if (instanceof(r, cl_I_ring) && instanceof(i, cl_RA_ring))
836             return numeric(complex(r*TheRatio(i)->denominator, TheRatio(i)->numerator));
837         if (instanceof(r, cl_RA_ring) && instanceof(i, cl_I_ring))
838             return numeric(complex(TheRatio(r)->numerator, i*TheRatio(r)->denominator));
839         if (instanceof(r, cl_RA_ring) && instanceof(i, cl_RA_ring)) {
840             cl_I s = lcm(TheRatio(r)->denominator, TheRatio(i)->denominator);
841             return numeric(complex(TheRatio(r)->numerator*(exquo(s,TheRatio(r)->denominator)),
842                                    TheRatio(i)->numerator*(exquo(s,TheRatio(i)->denominator))));
843         }
844     }
845 #endif // def SANE_LINKER
846     // at least one float encountered
847     return numeric(*this);
848 }
849
850 /** Denominator.  Computes the denominator of rational numbers, common integer
851  *  denominator of complex if real and imaginary part are both rational numbers
852  *  (i.e denom(4/3+5/6*I) == 6), one in all other cases. */
853 numeric numeric::denom(void) const
854 {
855     if (is_integer()) {
856         return _num1();
857     }
858 #ifdef SANE_LINKER
859     if (instanceof(*value, cl_RA_ring)) {
860         return numeric(::denominator(The(cl_RA)(*value)));
861     }
862     if (!is_real()) {  // complex case, handle Q(i):
863         cl_R r = realpart(*value);
864         cl_R i = imagpart(*value);
865         if (::instanceof(r, cl_I_ring) && ::instanceof(i, cl_I_ring))
866             return _num1();
867         if (::instanceof(r, cl_I_ring) && ::instanceof(i, cl_RA_ring))
868             return numeric(::denominator(The(cl_RA)(i)));
869         if (::instanceof(r, cl_RA_ring) && ::instanceof(i, cl_I_ring))
870             return numeric(::denominator(The(cl_RA)(r)));
871         if (::instanceof(r, cl_RA_ring) && ::instanceof(i, cl_RA_ring))
872             return numeric(lcm(::denominator(The(cl_RA)(r)), ::denominator(The(cl_RA)(i))));
873     }
874 #else
875     if (instanceof(*value, cl_RA_ring)) {
876         return numeric(TheRatio(*value)->denominator);
877     }
878     if (!is_real()) {  // complex case, handle Q(i):
879         cl_R r = realpart(*value);
880         cl_R i = imagpart(*value);
881         if (instanceof(r, cl_I_ring) && instanceof(i, cl_I_ring))
882             return _num1();
883         if (instanceof(r, cl_I_ring) && instanceof(i, cl_RA_ring))
884             return numeric(TheRatio(i)->denominator);
885         if (instanceof(r, cl_RA_ring) && instanceof(i, cl_I_ring))
886             return numeric(TheRatio(r)->denominator);
887         if (instanceof(r, cl_RA_ring) && instanceof(i, cl_RA_ring))
888             return numeric(lcm(TheRatio(r)->denominator, TheRatio(i)->denominator));
889     }
890 #endif // def SANE_LINKER
891     // at least one float encountered
892     return _num1();
893 }
894
895 /** Size in binary notation.  For integers, this is the smallest n >= 0 such
896  *  that -2^n <= x < 2^n. If x > 0, this is the unique n > 0 such that
897  *  2^(n-1) <= x < 2^n.
898  *
899  *  @return  number of bits (excluding sign) needed to represent that number
900  *  in two's complement if it is an integer, 0 otherwise. */    
901 int numeric::int_length(void) const
902 {
903     if (is_integer())
904         return ::integer_length(The(cl_I)(*value));  // -> CLN
905     else
906         return 0;
907 }
908
909
910 //////////
911 // static member variables
912 //////////
913
914 // protected
915
916 unsigned numeric::precedence = 30;
917
918 //////////
919 // global constants
920 //////////
921
922 const numeric some_numeric;
923 type_info const & typeid_numeric=typeid(some_numeric);
924 /** Imaginary unit.  This is not a constant but a numeric since we are
925  *  natively handing complex numbers anyways. */
926 const numeric I = numeric(complex(cl_I(0),cl_I(1)));
927
928 /** Exponential function.
929  *
930  *  @return  arbitrary precision numerical exp(x). */
931 numeric exp(const numeric & x)
932 {
933     return ::exp(*x.value);  // -> CLN
934 }
935
936 /** Natural logarithm.
937  *
938  *  @param z complex number
939  *  @return  arbitrary precision numerical log(x).
940  *  @exception overflow_error (logarithmic singularity) */
941 numeric log(const numeric & z)
942 {
943     if (z.is_zero())
944         throw (std::overflow_error("log(): logarithmic singularity"));
945     return ::log(*z.value);  // -> CLN
946 }
947
948 /** Numeric sine (trigonometric function).
949  *
950  *  @return  arbitrary precision numerical sin(x). */
951 numeric sin(const numeric & x)
952 {
953     return ::sin(*x.value);  // -> CLN
954 }
955
956 /** Numeric cosine (trigonometric function).
957  *
958  *  @return  arbitrary precision numerical cos(x). */
959 numeric cos(const numeric & x)
960 {
961     return ::cos(*x.value);  // -> CLN
962 }
963     
964 /** Numeric tangent (trigonometric function).
965  *
966  *  @return  arbitrary precision numerical tan(x). */
967 numeric tan(const numeric & x)
968 {
969     return ::tan(*x.value);  // -> CLN
970 }
971     
972 /** Numeric inverse sine (trigonometric function).
973  *
974  *  @return  arbitrary precision numerical asin(x). */
975 numeric asin(const numeric & x)
976 {
977     return ::asin(*x.value);  // -> CLN
978 }
979     
980 /** Numeric inverse cosine (trigonometric function).
981  *
982  *  @return  arbitrary precision numerical acos(x). */
983 numeric acos(const numeric & x)
984 {
985     return ::acos(*x.value);  // -> CLN
986 }
987     
988 /** Arcustangents.
989  *
990  *  @param z complex number
991  *  @return atan(z)
992  *  @exception overflow_error (logarithmic singularity) */
993 numeric atan(const numeric & x)
994 {
995     if (!x.is_real() &&
996         x.real().is_zero() &&
997         !abs(x.imag()).is_equal(_num1()))
998         throw (std::overflow_error("atan(): logarithmic singularity"));
999     return ::atan(*x.value);  // -> CLN
1000 }
1001
1002 /** Arcustangents.
1003  *
1004  *  @param x real number
1005  *  @param y real number
1006  *  @return atan(y/x) */
1007 numeric atan(const numeric & y, const numeric & x)
1008 {
1009     if (x.is_real() && y.is_real())
1010         return ::atan(realpart(*x.value), realpart(*y.value));  // -> CLN
1011     else
1012         throw (std::invalid_argument("numeric::atan(): complex argument"));        
1013 }
1014
1015 /** Numeric hyperbolic sine (trigonometric function).
1016  *
1017  *  @return  arbitrary precision numerical sinh(x). */
1018 numeric sinh(const numeric & x)
1019 {
1020     return ::sinh(*x.value);  // -> CLN
1021 }
1022
1023 /** Numeric hyperbolic cosine (trigonometric function).
1024  *
1025  *  @return  arbitrary precision numerical cosh(x). */
1026 numeric cosh(const numeric & x)
1027 {
1028     return ::cosh(*x.value);  // -> CLN
1029 }
1030     
1031 /** Numeric hyperbolic tangent (trigonometric function).
1032  *
1033  *  @return  arbitrary precision numerical tanh(x). */
1034 numeric tanh(const numeric & x)
1035 {
1036     return ::tanh(*x.value);  // -> CLN
1037 }
1038     
1039 /** Numeric inverse hyperbolic sine (trigonometric function).
1040  *
1041  *  @return  arbitrary precision numerical asinh(x). */
1042 numeric asinh(const numeric & x)
1043 {
1044     return ::asinh(*x.value);  // -> CLN
1045 }
1046
1047 /** Numeric inverse hyperbolic cosine (trigonometric function).
1048  *
1049  *  @return  arbitrary precision numerical acosh(x). */
1050 numeric acosh(const numeric & x)
1051 {
1052     return ::acosh(*x.value);  // -> CLN
1053 }
1054
1055 /** Numeric inverse hyperbolic tangent (trigonometric function).
1056  *
1057  *  @return  arbitrary precision numerical atanh(x). */
1058 numeric atanh(const numeric & x)
1059 {
1060     return ::atanh(*x.value);  // -> CLN
1061 }
1062
1063 /** Numeric evaluation of Riemann's Zeta function.  Currently works only for
1064  *  integer arguments. */
1065 numeric zeta(const numeric & x)
1066 {
1067     // A dirty hack to allow for things like zeta(3.0), since CLN currently
1068     // only knows about integer arguments and zeta(3).evalf() automatically
1069     // cascades down to zeta(3.0).evalf().  The trick is to rely on 3.0-3
1070     // being an exact zero for CLN, which can be tested and then we can just
1071     // pass the number casted to an int:
1072     if (x.is_real()) {
1073         int aux = (int)(::cl_double_approx(realpart(*x.value)));
1074         if (zerop(*x.value-aux))
1075             return ::cl_zeta(aux);  // -> CLN
1076     }
1077     clog << "zeta(" << x
1078          << "): Does anybody know good way to calculate this numerically?"
1079          << endl;
1080     return numeric(0);
1081 }
1082
1083 /** The gamma function.
1084  *  This is only a stub! */
1085 numeric gamma(const numeric & x)
1086 {
1087     clog << "gamma(" << x
1088          << "): Does anybody know good way to calculate this numerically?"
1089          << endl;
1090     return numeric(0);
1091 }
1092
1093 /** The psi function (aka polygamma function).
1094  *  This is only a stub! */
1095 numeric psi(const numeric & x)
1096 {
1097     clog << "psi(" << x
1098          << "): Does anybody know good way to calculate this numerically?"
1099          << endl;
1100     return numeric(0);
1101 }
1102
1103 /** The psi functions (aka polygamma functions).
1104  *  This is only a stub! */
1105 numeric psi(const numeric & n, const numeric & x)
1106 {
1107     clog << "psi(" << n << "," << x
1108          << "): Does anybody know good way to calculate this numerically?"
1109          << endl;
1110     return numeric(0);
1111 }
1112
1113 /** Factorial combinatorial function.
1114  *
1115  *  @exception range_error (argument must be integer >= 0) */
1116 numeric factorial(const numeric & nn)
1117 {
1118     if (!nn.is_nonneg_integer())
1119         throw (std::range_error("numeric::factorial(): argument must be integer >= 0"));
1120     return numeric(::factorial(nn.to_int()));  // -> CLN
1121 }
1122
1123 /** The double factorial combinatorial function.  (Scarcely used, but still
1124  *  useful in cases, like for exact results of Gamma(n+1/2) for instance.)
1125  *
1126  *  @param n  integer argument >= -1
1127  *  @return n!! == n * (n-2) * (n-4) * ... * ({1|2}) with 0!! == (-1)!! == 1
1128  *  @exception range_error (argument must be integer >= -1) */
1129 numeric doublefactorial(const numeric & nn)
1130 {
1131     if (nn == numeric(-1)) {
1132         return _num1();
1133     }
1134     if (!nn.is_nonneg_integer()) {
1135         throw (std::range_error("numeric::doublefactorial(): argument must be integer >= -1"));
1136     }
1137     return numeric(::doublefactorial(nn.to_int()));  // -> CLN
1138 }
1139
1140 /** The Binomial coefficients.  It computes the binomial coefficients.  For
1141  *  integer n and k and positive n this is the number of ways of choosing k
1142  *  objects from n distinct objects.  If n is negative, the formula
1143  *  binomial(n,k) == (-1)^k*binomial(k-n-1,k) is used to compute the result. */
1144 numeric binomial(const numeric & n, const numeric & k)
1145 {
1146     if (n.is_integer() && k.is_integer()) {
1147         if (n.is_nonneg_integer()) {
1148             if (k.compare(n)!=1 && k.compare(_num0())!=-1)
1149                 return numeric(::binomial(n.to_int(),k.to_int()));  // -> CLN
1150             else
1151                 return _num0();
1152         } else {
1153             return _num_1().power(k)*binomial(k-n-_num1(),k);
1154         }
1155     }
1156     
1157     // should really be gamma(n+1)/(gamma(r+1)/gamma(n-r+1) or a suitable limit
1158     throw (std::range_error("numeric::binomial(): donĀ“t know how to evaluate that."));
1159 }
1160
1161 /** Bernoulli number.  The nth Bernoulli number is the coefficient of x^n/n!
1162  *  in the expansion of the function x/(e^x-1).
1163  *
1164  *  @return the nth Bernoulli number (a rational number).
1165  *  @exception range_error (argument must be integer >= 0) */
1166 numeric bernoulli(const numeric & nn)
1167 {
1168     if (!nn.is_integer() || nn.is_negative())
1169         throw (std::range_error("numeric::bernoulli(): argument must be integer >= 0"));
1170     if (nn.is_zero())
1171         return _num1();
1172     if (!nn.compare(_num1()))
1173         return numeric(-1,2);
1174     if (nn.is_odd())
1175         return _num0();
1176     // Until somebody has the Blues and comes up with a much better idea and
1177     // codes it (preferably in CLN) we make this a remembering function which
1178     // computes its results using the formula
1179     // B(nn) == - 1/(nn+1) * sum_{k=0}^{nn-1}(binomial(nn+1,k)*B(k))
1180     // whith B(0) == 1.
1181     static vector<numeric> results;
1182     static int highest_result = -1;
1183     int n = nn.sub(_num2()).div(_num2()).to_int();
1184     if (n <= highest_result)
1185         return results[n];
1186     if (results.capacity() < (unsigned)(n+1))
1187         results.reserve(n+1);
1188     
1189     numeric tmp;  // used to store the sum
1190     for (int i=highest_result+1; i<=n; ++i) {
1191         // the first two elements:
1192         tmp = numeric(-2*i-1,2);
1193         // accumulate the remaining elements:
1194         for (int j=0; j<i; ++j)
1195             tmp += binomial(numeric(2*i+3),numeric(j*2+2))*results[j];
1196         // divide by -(nn+1) and store result:
1197         results.push_back(-tmp/numeric(2*i+3));
1198     }
1199     highest_result=n;
1200     return results[n];
1201 }
1202
1203 /** Absolute value. */
1204 numeric abs(const numeric & x)
1205 {
1206     return ::abs(*x.value);  // -> CLN
1207 }
1208
1209 /** Modulus (in positive representation).
1210  *  In general, mod(a,b) has the sign of b or is zero, and rem(a,b) has the
1211  *  sign of a or is zero. This is different from Maple's modp, where the sign
1212  *  of b is ignored. It is in agreement with Mathematica's Mod.
1213  *
1214  *  @return a mod b in the range [0,abs(b)-1] with sign of b if both are
1215  *  integer, 0 otherwise. */
1216 numeric mod(const numeric & a, const numeric & b)
1217 {
1218     if (a.is_integer() && b.is_integer())
1219         return ::mod(The(cl_I)(*a.value), The(cl_I)(*b.value));  // -> CLN
1220     else
1221         return _num0();  // Throw?
1222 }
1223
1224 /** Modulus (in symmetric representation).
1225  *  Equivalent to Maple's mods.
1226  *
1227  *  @return a mod b in the range [-iquo(abs(m)-1,2), iquo(abs(m),2)]. */
1228 numeric smod(const numeric & a, const numeric & b)
1229 {
1230     //  FIXME: Should this become a member function?
1231     if (a.is_integer() && b.is_integer()) {
1232         cl_I b2 = The(cl_I)(ceiling1(The(cl_I)(*b.value) / 2)) - 1;
1233         return ::mod(The(cl_I)(*a.value) + b2, The(cl_I)(*b.value)) - b2;
1234     } else
1235         return _num0();  // Throw?
1236 }
1237
1238 /** Numeric integer remainder.
1239  *  Equivalent to Maple's irem(a,b) as far as sign conventions are concerned.
1240  *  In general, mod(a,b) has the sign of b or is zero, and irem(a,b) has the
1241  *  sign of a or is zero.
1242  *
1243  *  @return remainder of a/b if both are integer, 0 otherwise. */
1244 numeric irem(const numeric & a, const numeric & b)
1245 {
1246     if (a.is_integer() && b.is_integer())
1247         return ::rem(The(cl_I)(*a.value), The(cl_I)(*b.value));  // -> CLN
1248     else
1249         return _num0();  // Throw?
1250 }
1251
1252 /** Numeric integer remainder.
1253  *  Equivalent to Maple's irem(a,b,'q') it obeyes the relation
1254  *  irem(a,b,q) == a - q*b.  In general, mod(a,b) has the sign of b or is zero,
1255  *  and irem(a,b) has the sign of a or is zero.  
1256  *
1257  *  @return remainder of a/b and quotient stored in q if both are integer,
1258  *  0 otherwise. */
1259 numeric irem(const numeric & a, const numeric & b, numeric & q)
1260 {
1261     if (a.is_integer() && b.is_integer()) {  // -> CLN
1262         cl_I_div_t rem_quo = truncate2(The(cl_I)(*a.value), The(cl_I)(*b.value));
1263         q = rem_quo.quotient;
1264         return rem_quo.remainder;
1265     }
1266     else {
1267         q = _num0();
1268         return _num0();  // Throw?
1269     }
1270 }
1271
1272 /** Numeric integer quotient.
1273  *  Equivalent to Maple's iquo as far as sign conventions are concerned.
1274  *  
1275  *  @return truncated quotient of a/b if both are integer, 0 otherwise. */
1276 numeric iquo(const numeric & a, const numeric & b)
1277 {
1278     if (a.is_integer() && b.is_integer())
1279         return truncate1(The(cl_I)(*a.value), The(cl_I)(*b.value));  // -> CLN
1280     else
1281         return _num0();  // Throw?
1282 }
1283
1284 /** Numeric integer quotient.
1285  *  Equivalent to Maple's iquo(a,b,'r') it obeyes the relation
1286  *  r == a - iquo(a,b,r)*b.
1287  *
1288  *  @return truncated quotient of a/b and remainder stored in r if both are
1289  *  integer, 0 otherwise. */
1290 numeric iquo(const numeric & a, const numeric & b, numeric & r)
1291 {
1292     if (a.is_integer() && b.is_integer()) {  // -> CLN
1293         cl_I_div_t rem_quo = truncate2(The(cl_I)(*a.value), The(cl_I)(*b.value));
1294         r = rem_quo.remainder;
1295         return rem_quo.quotient;
1296     } else {
1297         r = _num0();
1298         return _num0();  // Throw?
1299     }
1300 }
1301
1302 /** Numeric square root.
1303  *  If possible, sqrt(z) should respect squares of exact numbers, i.e. sqrt(4)
1304  *  should return integer 2.
1305  *
1306  *  @param z numeric argument
1307  *  @return square root of z. Branch cut along negative real axis, the negative
1308  *  real axis itself where imag(z)==0 and real(z)<0 belongs to the upper part
1309  *  where imag(z)>0. */
1310 numeric sqrt(const numeric & z)
1311 {
1312     return ::sqrt(*z.value);  // -> CLN
1313 }
1314
1315 /** Integer numeric square root. */
1316 numeric isqrt(const numeric & x)
1317 {
1318     if (x.is_integer()) {
1319         cl_I root;
1320         ::isqrt(The(cl_I)(*x.value), &root);  // -> CLN
1321         return root;
1322     } else
1323         return _num0();  // Throw?
1324 }
1325
1326 /** Greatest Common Divisor.
1327  *   
1328  *  @return  The GCD of two numbers if both are integer, a numerical 1
1329  *  if they are not. */
1330 numeric gcd(const numeric & a, const numeric & b)
1331 {
1332     if (a.is_integer() && b.is_integer())
1333         return ::gcd(The(cl_I)(*a.value), The(cl_I)(*b.value));  // -> CLN
1334     else
1335         return _num1();
1336 }
1337
1338 /** Least Common Multiple.
1339  *   
1340  *  @return  The LCM of two numbers if both are integer, the product of those
1341  *  two numbers if they are not. */
1342 numeric lcm(const numeric & a, const numeric & b)
1343 {
1344     if (a.is_integer() && b.is_integer())
1345         return ::lcm(The(cl_I)(*a.value), The(cl_I)(*b.value));  // -> CLN
1346     else
1347         return *a.value * *b.value;
1348 }
1349
1350 ex PiEvalf(void)
1351
1352     return numeric(cl_pi(cl_default_float_format));  // -> CLN
1353 }
1354
1355 ex EulerGammaEvalf(void)
1356
1357     return numeric(cl_eulerconst(cl_default_float_format));  // -> CLN
1358 }
1359
1360 ex CatalanEvalf(void)
1361 {
1362     return numeric(cl_catalanconst(cl_default_float_format));  // -> CLN
1363 }
1364
1365 // It initializes to 17 digits, because in CLN cl_float_format(17) turns out to
1366 // be 61 (<64) while cl_float_format(18)=65.  We want to have a cl_LF instead 
1367 // of cl_SF, cl_FF or cl_DF but everything else is basically arbitrary.
1368 _numeric_digits::_numeric_digits()
1369     : digits(17)
1370 {
1371     assert(!too_late);
1372     too_late = true;
1373     cl_default_float_format = cl_float_format(17); 
1374 }
1375
1376 _numeric_digits& _numeric_digits::operator=(long prec)
1377 {
1378     digits=prec;
1379     cl_default_float_format = cl_float_format(prec); 
1380     return *this;
1381 }
1382
1383 _numeric_digits::operator long()
1384 {
1385     return (long)digits;
1386 }
1387
1388 void _numeric_digits::print(ostream & os) const
1389 {
1390     debugmsg("_numeric_digits print", LOGLEVEL_PRINT);
1391     os << digits;
1392 }
1393
1394 ostream& operator<<(ostream& os, _numeric_digits const & e)
1395 {
1396     e.print(os);
1397     return os;
1398 }
1399
1400 //////////
1401 // static member variables
1402 //////////
1403
1404 // private
1405
1406 bool _numeric_digits::too_late = false;
1407
1408 /** Accuracy in decimal digits.  Only object of this type!  Can be set using
1409  *  assignment from C++ unsigned ints and evaluated like any built-in type. */
1410 _numeric_digits Digits;
1411
1412 #ifndef NO_GINAC_NAMESPACE
1413 } // namespace GiNaC
1414 #endif // ndef NO_GINAC_NAMESPACE