// str and repr // For now, just print the same way as ordinary doubles. // TODO this is wrong, e.g. if real or imaginary part is an integer, there should // be no decimal point, maybe some other differences. Need to dig deeper into // how CPython formats floats and complex numbers. // (complex_format in Objects/complexobject.c) std::string complexFmt(double r, double i, int precision, char code) { if (r == 0. && copysign(1.0, r) == 1.0) { return floatFmt(i, precision, code) + "j"; } else { return "(" + floatFmt(r, precision, code) + (isnan(i) || i >= 0.0 ? "+" : "") + floatFmt(i, precision, code) + "j)"; } }
extern "C" void printFloat(double d) { std::string s = floatFmt(d, 12, 'g'); printf("%s", s.c_str()); }
Box* floatRepr(BoxedFloat *self) { assert(self->cls == float_cls); return boxString(floatFmt(self->d, 16, 'g')); }