-bool numeric::info(unsigned inf) const
-{
- switch (inf) {
- case info_flags::numeric:
- case info_flags::polynomial:
- case info_flags::rational_function:
- return true;
- case info_flags::real:
- return is_real();
- case info_flags::rational:
- case info_flags::rational_polynomial:
- return is_rational();
- case info_flags::crational:
- case info_flags::crational_polynomial:
- return is_crational();
- case info_flags::integer:
- case info_flags::integer_polynomial:
- return is_integer();
- case info_flags::cinteger:
- case info_flags::cinteger_polynomial:
- return is_cinteger();
- case info_flags::positive:
- return is_positive();
- case info_flags::negative:
- return is_negative();
- case info_flags::nonnegative:
- return compare(_num0())>=0;
- case info_flags::posint:
- return is_pos_integer();
- case info_flags::negint:
- return is_integer() && (compare(_num0())<0);
- case info_flags::nonnegint:
- return is_nonneg_integer();
- case info_flags::even:
- return is_even();
- case info_flags::odd:
- return is_odd();
- case info_flags::prime:
- return is_prime();
- }
- return false;
+
+/** Helper function to print a real number in a nicer way than is CLN's
+ * default. Instead of printing 42.0L0 this just prints 42.0 to ostream os
+ * and instead of 3.99168L7 it prints 3.99168E7. This is fine in GiNaC as
+ * long as it only uses cl_LF and no other floating point types that we might
+ * want to visibly distinguish from cl_LF.
+ *
+ * @see numeric::print() */
+static void print_real_number(std::ostream & os, const cln::cl_R & num)
+{
+ cln::cl_print_flags ourflags;
+ if (cln::instanceof(num, cln::cl_RA_ring)) {
+ // case 1: integer or rational, nothing special to do:
+ cln::print_real(os, ourflags, num);
+ } else {
+ // case 2: float
+ // make CLN believe this number has default_float_format, so it prints
+ // 'E' as exponent marker instead of 'L':
+ ourflags.default_float_format = cln::float_format(cln::the<cln::cl_F>(num));
+ cln::print_real(os, ourflags, num);
+ }
+ return;
+}
+
+/** This method adds to the output so it blends more consistently together
+ * with the other routines and produces something compatible to ginsh input.
+ *
+ * @see print_real_number() */
+void numeric::print(std::ostream & os, unsigned upper_precedence) const
+{
+ debugmsg("numeric print", LOGLEVEL_PRINT);
+ cln::cl_R r = cln::realpart(cln::the<cln::cl_N>(value));
+ cln::cl_R i = cln::imagpart(cln::the<cln::cl_N>(value));
+ if (cln::zerop(i)) {
+ // case 1, real: x or -x
+ if ((precedence<=upper_precedence) && (!this->is_nonneg_integer())) {
+ os << "(";
+ print_real_number(os, r);
+ os << ")";
+ } else {
+ print_real_number(os, r);
+ }
+ } else {
+ if (cln::zerop(r)) {
+ // case 2, imaginary: y*I or -y*I
+ if ((precedence<=upper_precedence) && (i < 0)) {
+ if (i == -1) {
+ os << "(-I)";
+ } else {
+ os << "(";
+ print_real_number(os, i);
+ os << "*I)";
+ }
+ } else {
+ if (i == 1) {
+ os << "I";
+ } else {
+ if (i == -1) {
+ os << "-I";
+ } else {
+ print_real_number(os, i);
+ os << "*I";
+ }
+ }
+ }
+ } else {
+ // case 3, complex: x+y*I or x-y*I or -x+y*I or -x-y*I
+ if (precedence <= upper_precedence)
+ os << "(";
+ print_real_number(os, r);
+ if (i < 0) {
+ if (i == -1) {
+ os << "-I";
+ } else {
+ print_real_number(os, i);
+ os << "*I";
+ }
+ } else {
+ if (i == 1) {
+ os << "+I";
+ } else {
+ os << "+";
+ print_real_number(os, i);
+ os << "*I";
+ }
+ }
+ if (precedence <= upper_precedence)
+ os << ")";
+ }
+ }
+}
+
+
+void numeric::printraw(std::ostream & os) const
+{
+ // The method printraw doesn't do much, it simply uses CLN's operator<<()
+ // for output, which is ugly but reliable. e.g: 2+2i
+ debugmsg("numeric printraw", LOGLEVEL_PRINT);
+ os << "numeric(" << cln::the<cln::cl_N>(value) << ")";
+}
+
+
+void numeric::printtree(std::ostream & os, unsigned indent) const
+{
+ debugmsg("numeric printtree", LOGLEVEL_PRINT);
+ os << std::string(indent,' ') << cln::the<cln::cl_N>(value)
+ << " (numeric): "
+ << "hash=" << hashvalue
+ << " (0x" << std::hex << hashvalue << std::dec << ")"
+ << ", flags=" << flags << std::endl;
+}
+
+
+void numeric::printcsrc(std::ostream & os, unsigned type, unsigned upper_precedence) const
+{
+ debugmsg("numeric print csrc", LOGLEVEL_PRINT);
+ std::ios::fmtflags oldflags = os.flags();
+ os.setf(std::ios::scientific);
+ if (this->is_rational() && !this->is_integer()) {
+ if (compare(_num0()) > 0) {
+ os << "(";
+ if (type == csrc_types::ctype_cl_N)
+ os << "cln::cl_F(\"" << numer().evalf() << "\")";
+ else
+ os << numer().to_double();
+ } else {
+ os << "-(";
+ if (type == csrc_types::ctype_cl_N)
+ os << "cln::cl_F(\"" << -numer().evalf() << "\")";
+ else
+ os << -numer().to_double();
+ }
+ os << "/";
+ if (type == csrc_types::ctype_cl_N)
+ os << "cln::cl_F(\"" << denom().evalf() << "\")";
+ else
+ os << denom().to_double();
+ os << ")";
+ } else {
+ if (type == csrc_types::ctype_cl_N)
+ os << "cln::cl_F(\"" << evalf() << "\")";
+ else
+ os << to_double();
+ }
+ os.flags(oldflags);