home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 15 / AACD15.ISO / AACD / Programming / Python2 / Python20_source / Objects / floatobject.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-10-25  |  16.7 KB  |  711 lines

  1.  
  2. /* Float object implementation */
  3.  
  4. /* XXX There should be overflow checks here, but it's hard to check
  5.    for any kind of float exception without losing portability. */
  6.  
  7. #include "Python.h"
  8.  
  9. #include <ctype.h>
  10.  
  11. #ifdef i860
  12. /* Cray APP has bogus definition of HUGE_VAL in <math.h> */
  13. #undef HUGE_VAL
  14. #endif
  15.  
  16. #if defined(HUGE_VAL) && !defined(CHECK)
  17. #define CHECK(x) if (errno != 0) ; \
  18.     else if (-HUGE_VAL <= (x) && (x) <= HUGE_VAL) ; \
  19.     else errno = ERANGE
  20. #endif
  21.  
  22. #ifndef CHECK
  23. #define CHECK(x) /* Don't know how to check */
  24. #endif
  25.  
  26. #if !defined(__STDC__) && !defined(macintosh)
  27. extern double fmod(double, double);
  28. extern double pow(double, double);
  29. #endif
  30.  
  31. #ifdef sun
  32. /* On SunOS4.1 only libm.a exists. Make sure that references to all
  33.    needed math functions exist in the executable, so that dynamic
  34.    loading of mathmodule does not fail. */
  35. double (*_Py_math_funcs_hack[])() = {
  36.     acos, asin, atan, atan2, ceil, cos, cosh, exp, fabs, floor,
  37.     fmod, log, log10, pow, sin, sinh, sqrt, tan, tanh
  38. };
  39. #endif
  40.  
  41. /* Special free list -- see comments for same code in intobject.c. */
  42. #define BLOCK_SIZE    1000    /* 1K less typical malloc overhead */
  43. #define BHEAD_SIZE    8    /* Enough for a 64-bit pointer */
  44. #define N_FLOATOBJECTS    ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyFloatObject))
  45.  
  46. struct _floatblock {
  47.     struct _floatblock *next;
  48.     PyFloatObject objects[N_FLOATOBJECTS];
  49. };
  50.  
  51. typedef struct _floatblock PyFloatBlock;
  52.  
  53. static PyFloatBlock *block_list = NULL;
  54. static PyFloatObject *free_list = NULL;
  55.  
  56. static PyFloatObject *
  57. fill_free_list(void)
  58. {
  59.     PyFloatObject *p, *q;
  60.     /* XXX Float blocks escape the object heap. Use PyObject_MALLOC ??? */
  61.     p = (PyFloatObject *) PyMem_MALLOC(sizeof(PyFloatBlock));
  62.     if (p == NULL)
  63.         return (PyFloatObject *) PyErr_NoMemory();
  64.     ((PyFloatBlock *)p)->next = block_list;
  65.     block_list = (PyFloatBlock *)p;
  66.     p = &((PyFloatBlock *)p)->objects[0];
  67.     q = p + N_FLOATOBJECTS;
  68.     while (--q > p)
  69.         q->ob_type = (struct _typeobject *)(q-1);
  70.     q->ob_type = NULL;
  71.     return p + N_FLOATOBJECTS - 1;
  72. }
  73.  
  74. PyObject *
  75. PyFloat_FromDouble(double fval)
  76. {
  77.     register PyFloatObject *op;
  78.     if (free_list == NULL) {
  79.         if ((free_list = fill_free_list()) == NULL)
  80.             return NULL;
  81.     }
  82.     /* PyObject_New is inlined */
  83.     op = free_list;
  84.     free_list = (PyFloatObject *)op->ob_type;
  85.     PyObject_INIT(op, &PyFloat_Type);
  86.     op->ob_fval = fval;
  87.     return (PyObject *) op;
  88. }
  89.  
  90. /**************************************************************************
  91. RED_FLAG 22-Sep-2000 tim
  92. PyFloat_FromString's pend argument is braindead.  Prior to this RED_FLAG,
  93.  
  94. 1.  If v was a regular string, *pend was set to point to its terminating
  95.     null byte.  That's useless (the caller can find that without any
  96.     help from this function!).
  97.  
  98. 2.  If v was a Unicode string, or an object convertible to a character
  99.     buffer, *pend was set to point into stack trash (the auto temp
  100.     vector holding the character buffer).  That was downright dangerous.
  101.  
  102. Since we can't change the interface of a public API function, pend is
  103. still supported but now *officially* useless:  if pend is not NULL,
  104. *pend is set to NULL.
  105. **************************************************************************/
  106. PyObject *
  107. PyFloat_FromString(PyObject *v, char **pend)
  108. {
  109.     const char *s, *last, *end;
  110.     double x;
  111.     char buffer[256]; /* for errors */
  112.     char s_buffer[256]; /* for objects convertible to a char buffer */
  113.     int len;
  114.  
  115.     if (pend)
  116.         *pend = NULL;
  117.     if (PyString_Check(v)) {
  118.         s = PyString_AS_STRING(v);
  119.         len = PyString_GET_SIZE(v);
  120.     }
  121.     else if (PyUnicode_Check(v)) {
  122.         if (PyUnicode_GET_SIZE(v) >= sizeof(s_buffer)) {
  123.             PyErr_SetString(PyExc_ValueError,
  124.                 "Unicode float() literal too long to convert");
  125.             return NULL;
  126.         }
  127.         if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v),
  128.                         PyUnicode_GET_SIZE(v),
  129.                         s_buffer, 
  130.                         NULL))
  131.             return NULL;
  132.         s = s_buffer;
  133.         len = (int)strlen(s);
  134.     }
  135.     else if (PyObject_AsCharBuffer(v, &s, &len)) {
  136.         PyErr_SetString(PyExc_TypeError,
  137.                 "float() needs a string argument");
  138.         return NULL;
  139.     }
  140.  
  141.     last = s + len;
  142.     while (*s && isspace(Py_CHARMASK(*s)))
  143.         s++;
  144.     if (*s == '\0') {
  145.         PyErr_SetString(PyExc_ValueError, "empty string for float()");
  146.         return NULL;
  147.     }
  148.     /* We don't care about overflow or underflow.  If the platform supports
  149.      * them, infinities and signed zeroes (on underflow) are fine.
  150.      * However, strtod can return 0 for denormalized numbers, where atof
  151.      * does not.  So (alas!) we special-case a zero result.  Note that
  152.      * whether strtod sets errno on underflow is not defined, so we can't
  153.      * key off errno.
  154.          */
  155.     PyFPE_START_PROTECT("strtod", return NULL)
  156.     x = strtod(s, (char **)&end);
  157.     PyFPE_END_PROTECT(x)
  158.     errno = 0;
  159.     /* Believe it or not, Solaris 2.6 can move end *beyond* the null
  160.        byte at the end of the string, when the input is inf(inity). */
  161.     if (end > last)
  162.         end = last;
  163.     if (end == s) {
  164.         sprintf(buffer, "invalid literal for float(): %.200s", s);
  165.         PyErr_SetString(PyExc_ValueError, buffer);
  166.         return NULL;
  167.     }
  168.     /* Since end != s, the platform made *some* kind of sense out
  169.        of the input.  Trust it. */
  170.     while (*end && isspace(Py_CHARMASK(*end)))
  171.         end++;
  172.     if (*end != '\0') {
  173.         sprintf(buffer, "invalid literal for float(): %.200s", s);
  174.         PyErr_SetString(PyExc_ValueError, buffer);
  175.         return NULL;
  176.     }
  177.     else if (end != last) {
  178.         PyErr_SetString(PyExc_ValueError,
  179.                 "null byte in argument for float()");
  180.         return NULL;
  181.     }
  182.     if (x == 0.0) {
  183.         /* See above -- may have been strtod being anal
  184.            about denorms. */
  185.         PyFPE_START_PROTECT("atof", return NULL)
  186.         x = atof(s);
  187.         PyFPE_END_PROTECT(x)
  188.         errno = 0;    /* whether atof ever set errno is undefined */
  189.     }
  190.     return PyFloat_FromDouble(x);
  191. }
  192.  
  193. static void
  194. float_dealloc(PyFloatObject *op)
  195. {
  196.     op->ob_type = (struct _typeobject *)free_list;
  197.     free_list = op;
  198. }
  199.  
  200. double
  201. PyFloat_AsDouble(PyObject *op)
  202. {
  203.     PyNumberMethods *nb;
  204.     PyFloatObject *fo;
  205.     double val;
  206.     
  207.     if (op && PyFloat_Check(op))
  208.         return PyFloat_AS_DOUBLE((PyFloatObject*) op);
  209.     
  210.     if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
  211.         nb->nb_float == NULL) {
  212.         PyErr_BadArgument();
  213.         return -1;
  214.     }
  215.     
  216.     fo = (PyFloatObject*) (*nb->nb_float) (op);
  217.     if (fo == NULL)
  218.         return -1;
  219.     if (!PyFloat_Check(fo)) {
  220.         PyErr_SetString(PyExc_TypeError,
  221.                 "nb_float should return float object");
  222.         return -1;
  223.     }
  224.     
  225.     val = PyFloat_AS_DOUBLE(fo);
  226.     Py_DECREF(fo);
  227.     
  228.     return val;
  229. }
  230.  
  231. /* Methods */
  232.  
  233. void
  234. PyFloat_AsStringEx(char *buf, PyFloatObject *v, int precision)
  235. {
  236.     register char *cp;
  237.     /* Subroutine for float_repr and float_print.
  238.        We want float numbers to be recognizable as such,
  239.        i.e., they should contain a decimal point or an exponent.
  240.        However, %g may print the number as an integer;
  241.        in such cases, we append ".0" to the string. */
  242.     sprintf(buf, "%.*g", precision, v->ob_fval);
  243.     cp = buf;
  244.     if (*cp == '-')
  245.         cp++;
  246.     for (; *cp != '\0'; cp++) {
  247.         /* Any non-digit means it's not an integer;
  248.            this takes care of NAN and INF as well. */
  249.         if (!isdigit(Py_CHARMASK(*cp)))
  250.             break;
  251.     }
  252.     if (*cp == '\0') {
  253.         *cp++ = '.';
  254.         *cp++ = '0';
  255.         *cp++ = '\0';
  256.     }
  257. }
  258.  
  259. /* Precisions used by repr() and str(), respectively.
  260.  
  261.    The repr() precision (17 significant decimal digits) is the minimal number
  262.    that is guaranteed to have enough precision so that if the number is read
  263.    back in the exact same binary value is recreated.  This is true for IEEE
  264.    floating point by design, and also happens to work for all other modern
  265.    hardware.
  266.  
  267.    The str() precision is chosen so that in most cases, the rounding noise
  268.    created by various operations is suppressed, while giving plenty of
  269.    precision for practical use.
  270.  
  271. */
  272.  
  273. #define PREC_REPR    17
  274. #define PREC_STR    12
  275.  
  276. void
  277. PyFloat_AsString(char *buf, PyFloatObject *v)
  278. {
  279.     PyFloat_AsStringEx(buf, v, PREC_STR);
  280. }
  281.  
  282. /* ARGSUSED */
  283. static int
  284. float_print(PyFloatObject *v, FILE *fp, int flags)
  285.      /* flags -- not used but required by interface */
  286. {
  287.     char buf[100];
  288.     PyFloat_AsStringEx(buf, v, flags&Py_PRINT_RAW ? PREC_STR : PREC_REPR);
  289.     fputs(buf, fp);
  290.     return 0;
  291. }
  292.  
  293. static PyObject *
  294. float_repr(PyFloatObject *v)
  295. {
  296.     char buf[100];
  297.     PyFloat_AsStringEx(buf, v, PREC_REPR);
  298.     return PyString_FromString(buf);
  299. }
  300.  
  301. static PyObject *
  302. float_str(PyFloatObject *v)
  303. {
  304.     char buf[100];
  305.     PyFloat_AsStringEx(buf, v, PREC_STR);
  306.     return PyString_FromString(buf);
  307. }
  308.  
  309. static int
  310. float_compare(PyFloatObject *v, PyFloatObject *w)
  311. {
  312.     double i = v->ob_fval;
  313.     double j = w->ob_fval;
  314.     return (i < j) ? -1 : (i > j) ? 1 : 0;
  315. }
  316.  
  317.  
  318. static long
  319. float_hash(PyFloatObject *v)
  320. {
  321.     return _Py_HashDouble(v->ob_fval);
  322. }
  323.  
  324. static PyObject *
  325. float_add(PyFloatObject *v, PyFloatObject *w)
  326. {
  327.     double result;
  328.     PyFPE_START_PROTECT("add", return 0)
  329.     result = v->ob_fval + w->ob_fval;
  330.     PyFPE_END_PROTECT(result)
  331.     return PyFloat_FromDouble(result);
  332. }
  333.  
  334. static PyObject *
  335. float_sub(PyFloatObject *v, PyFloatObject *w)
  336. {
  337.     double result;
  338.     PyFPE_START_PROTECT("subtract", return 0)
  339.     result = v->ob_fval - w->ob_fval;
  340.     PyFPE_END_PROTECT(result)
  341.     return PyFloat_FromDouble(result);
  342. }
  343.  
  344. static PyObject *
  345. float_mul(PyFloatObject *v, PyFloatObject *w)
  346. {
  347.     double result;
  348.  
  349.     PyFPE_START_PROTECT("multiply", return 0)
  350.     result = v->ob_fval * w->ob_fval;
  351.     PyFPE_END_PROTECT(result)
  352.     return PyFloat_FromDouble(result);
  353. }
  354.  
  355. static PyObject *
  356. float_div(PyFloatObject *v, PyFloatObject *w)
  357. {
  358.     double result;
  359.     if (w->ob_fval == 0) {
  360.         PyErr_SetString(PyExc_ZeroDivisionError, "float division");
  361.         return NULL;
  362.     }
  363.     PyFPE_START_PROTECT("divide", return 0)
  364.     result = v->ob_fval / w->ob_fval;
  365.     PyFPE_END_PROTECT(result)
  366.     return PyFloat_FromDouble(result);
  367. }
  368.  
  369. static PyObject *
  370. float_rem(PyFloatObject *v, PyFloatObject *w)
  371. {
  372.     double vx, wx;
  373.     double mod;
  374.     wx = w->ob_fval;
  375.     if (wx == 0.0) {
  376.         PyErr_SetString(PyExc_ZeroDivisionError, "float modulo");
  377.         return NULL;
  378.     }
  379.     PyFPE_START_PROTECT("modulo", return 0)
  380.     vx = v->ob_fval;
  381.     mod = fmod(vx, wx);
  382.     /* note: checking mod*wx < 0 is incorrect -- underflows to
  383.        0 if wx < sqrt(smallest nonzero double) */
  384.     if (mod && ((wx < 0) != (mod < 0))) {
  385.         mod += wx;
  386.     }
  387.     PyFPE_END_PROTECT(mod)
  388.     return PyFloat_FromDouble(mod);
  389. }
  390.  
  391. static PyObject *
  392. float_divmod(PyFloatObject *v, PyFloatObject *w)
  393. {
  394.     double vx, wx;
  395.     double div, mod, floordiv;
  396.     wx = w->ob_fval;
  397.     if (wx == 0.0) {
  398.         PyErr_SetString(PyExc_ZeroDivisionError, "float divmod()");
  399.         return NULL;
  400.     }
  401.     PyFPE_START_PROTECT("divmod", return 0)
  402.     vx = v->ob_fval;
  403.     mod = fmod(vx, wx);
  404.     /* fmod is typically exact, so vx-mod is *mathematically* an
  405.        exact multiple of wx.  But this is fp arithmetic, and fp
  406.        vx - mod is an approximation; the result is that div may
  407.        not be an exact integral value after the division, although
  408.        it will always be very close to one.
  409.     */
  410.     div = (vx - mod) / wx;
  411.     /* note: checking mod*wx < 0 is incorrect -- underflows to
  412.        0 if wx < sqrt(smallest nonzero double) */
  413.     if (mod && ((wx < 0) != (mod < 0))) {
  414.         mod += wx;
  415.         div -= 1.0;
  416.     }
  417.     /* snap quotient to nearest integral value */
  418.     floordiv = floor(div);
  419.     if (div - floordiv > 0.5)
  420.         floordiv += 1.0;
  421.     PyFPE_END_PROTECT(div)
  422.     return Py_BuildValue("(dd)", floordiv, mod);
  423. }
  424.  
  425. static double powu(double x, long n)
  426. {
  427.     double r = 1.;
  428.     double p = x;
  429.     long mask = 1;
  430.     while (mask > 0 && n >= mask) {
  431.         if (n & mask)
  432.             r *= p;
  433.         mask <<= 1;
  434.         p *= p;
  435.     }
  436.     return r;
  437. }
  438.  
  439. static PyObject *
  440. float_pow(PyFloatObject *v, PyObject *w, PyFloatObject *z)
  441. {
  442.     double iv, iw, ix;
  443.     long intw;
  444.  /* XXX Doesn't handle overflows if z!=None yet; it may never do so :(
  445.   * The z parameter is really only going to be useful for integers and
  446.   * long integers.  Maybe something clever with logarithms could be done.
  447.   * [AMK]
  448.   */
  449.     iv = v->ob_fval;
  450.     iw = ((PyFloatObject *)w)->ob_fval;
  451.     intw = (long)iw;
  452.  
  453.     /* Sort out special cases here instead of relying on pow() */
  454.     if (iw == 0) {         /* x**0 is 1, even 0**0 */
  455.         PyFPE_START_PROTECT("pow", return NULL)
  456.         if ((PyObject *)z != Py_None) {
  457.             ix = fmod(1.0, z->ob_fval);
  458.             if (ix != 0 && z->ob_fval < 0)
  459.                 ix += z->ob_fval;
  460.         }
  461.         else
  462.             ix = 1.0;
  463.         PyFPE_END_PROTECT(ix)
  464.         return PyFloat_FromDouble(ix); 
  465.     }
  466.     if (iv == 0.0) {
  467.         if (iw < 0.0) {
  468.             PyErr_SetString(PyExc_ZeroDivisionError,
  469.                    "0.0 to a negative power");
  470.             return NULL;
  471.         }
  472.         return PyFloat_FromDouble(0.0);
  473.     }
  474.  
  475.     if (iw == intw && intw > LONG_MIN) {
  476.         /* ruled out LONG_MIN because -LONG_MIN isn't representable */
  477.         errno = 0;
  478.         PyFPE_START_PROTECT("pow", return NULL)
  479.         if (intw > 0)
  480.             ix = powu(iv, intw);
  481.         else
  482.             ix = 1./powu(iv, -intw);
  483.         PyFPE_END_PROTECT(ix)
  484.     }
  485.     else {
  486.         /* Sort out special cases here instead of relying on pow() */
  487.         if (iv < 0.0) {
  488.             PyErr_SetString(PyExc_ValueError,
  489.                    "negative number to a float power");
  490.             return NULL;
  491.         }
  492.         errno = 0;
  493.         PyFPE_START_PROTECT("pow", return NULL)
  494.         ix = pow(iv, iw);
  495.         PyFPE_END_PROTECT(ix)
  496.     }
  497.     CHECK(ix);
  498.     if (errno != 0) {
  499.         /* XXX could it be another type of error? */
  500.         PyErr_SetFromErrno(PyExc_OverflowError);
  501.         return NULL;
  502.     }
  503.     if ((PyObject *)z != Py_None) {
  504.         PyFPE_START_PROTECT("pow", return NULL)
  505.         ix = fmod(ix, z->ob_fval);    /* XXX To Be Rewritten */
  506.         if (ix != 0 &&
  507.             ((iv < 0 && z->ob_fval > 0) ||
  508.              (iv > 0 && z->ob_fval < 0)
  509.             )) {
  510.              ix += z->ob_fval;
  511.         }
  512.         PyFPE_END_PROTECT(ix)
  513.     }
  514.     return PyFloat_FromDouble(ix);
  515. }
  516.  
  517. static PyObject *
  518. float_neg(PyFloatObject *v)
  519. {
  520.     return PyFloat_FromDouble(-v->ob_fval);
  521. }
  522.  
  523. static PyObject *
  524. float_pos(PyFloatObject *v)
  525. {
  526.     Py_INCREF(v);
  527.     return (PyObject *)v;
  528. }
  529.  
  530. static PyObject *
  531. float_abs(PyFloatObject *v)
  532. {
  533.     if (v->ob_fval < 0)
  534.         return float_neg(v);
  535.     else
  536.         return float_pos(v);
  537. }
  538.  
  539. static int
  540. float_nonzero(PyFloatObject *v)
  541. {
  542.     return v->ob_fval != 0.0;
  543. }
  544.  
  545. static int
  546. float_coerce(PyObject **pv, PyObject **pw)
  547. {
  548.     if (PyInt_Check(*pw)) {
  549.         long x = PyInt_AsLong(*pw);
  550.         *pw = PyFloat_FromDouble((double)x);
  551.         Py_INCREF(*pv);
  552.         return 0;
  553.     }
  554.     else if (PyLong_Check(*pw)) {
  555.         *pw = PyFloat_FromDouble(PyLong_AsDouble(*pw));
  556.         Py_INCREF(*pv);
  557.         return 0;
  558.     }
  559.     return 1; /* Can't do it */
  560. }
  561.  
  562. static PyObject *
  563. float_int(PyObject *v)
  564. {
  565.     double x = PyFloat_AsDouble(v);
  566.     if (x < 0 ? (x = ceil(x)) < (double)LONG_MIN
  567.               : (x = floor(x)) > (double)LONG_MAX) {
  568.         PyErr_SetString(PyExc_OverflowError,
  569.                 "float too large to convert");
  570.         return NULL;
  571.     }
  572.     return PyInt_FromLong((long)x);
  573. }
  574.  
  575. static PyObject *
  576. float_long(PyObject *v)
  577. {
  578.     double x = PyFloat_AsDouble(v);
  579.     return PyLong_FromDouble(x);
  580. }
  581.  
  582. static PyObject *
  583. float_float(PyObject *v)
  584. {
  585.     Py_INCREF(v);
  586.     return v;
  587. }
  588.  
  589.  
  590. static PyNumberMethods float_as_number = {
  591.     (binaryfunc)float_add, /*nb_add*/
  592.     (binaryfunc)float_sub, /*nb_subtract*/
  593.     (binaryfunc)float_mul, /*nb_multiply*/
  594.     (binaryfunc)float_div, /*nb_divide*/
  595.     (binaryfunc)float_rem, /*nb_remainder*/
  596.     (binaryfunc)float_divmod, /*nb_divmod*/
  597.     (ternaryfunc)float_pow, /*nb_power*/
  598.     (unaryfunc)float_neg, /*nb_negative*/
  599.     (unaryfunc)float_pos, /*nb_positive*/
  600.     (unaryfunc)float_abs, /*nb_absolute*/
  601.     (inquiry)float_nonzero, /*nb_nonzero*/
  602.     0,        /*nb_invert*/
  603.     0,        /*nb_lshift*/
  604.     0,        /*nb_rshift*/
  605.     0,        /*nb_and*/
  606.     0,        /*nb_xor*/
  607.     0,        /*nb_or*/
  608.     (coercion)float_coerce, /*nb_coerce*/
  609.     (unaryfunc)float_int, /*nb_int*/
  610.     (unaryfunc)float_long, /*nb_long*/
  611.     (unaryfunc)float_float, /*nb_float*/
  612.     0,        /*nb_oct*/
  613.     0,        /*nb_hex*/
  614. };
  615.  
  616. PyTypeObject PyFloat_Type = {
  617.     PyObject_HEAD_INIT(&PyType_Type)
  618.     0,
  619.     "float",
  620.     sizeof(PyFloatObject),
  621.     0,
  622.     (destructor)float_dealloc, /*tp_dealloc*/
  623.     (printfunc)float_print, /*tp_print*/
  624.     0,            /*tp_getattr*/
  625.     0,            /*tp_setattr*/
  626.     (cmpfunc)float_compare, /*tp_compare*/
  627.     (reprfunc)float_repr,    /*tp_repr*/
  628.     &float_as_number,    /*tp_as_number*/
  629.     0,            /*tp_as_sequence*/
  630.     0,            /*tp_as_mapping*/
  631.     (hashfunc)float_hash,    /*tp_hash*/
  632.         0,            /*tp_call*/
  633.         (reprfunc)float_str,    /*tp_str*/
  634. };
  635.  
  636. void
  637. PyFloat_Fini(void)
  638. {
  639.     PyFloatObject *p;
  640.     PyFloatBlock *list, *next;
  641.     int i;
  642.     int bc, bf;    /* block count, number of freed blocks */
  643.     int frem, fsum;    /* remaining unfreed floats per block, total */
  644.  
  645.     bc = 0;
  646.     bf = 0;
  647.     fsum = 0;
  648.     list = block_list;
  649.     block_list = NULL;
  650.     free_list = NULL;
  651.     while (list != NULL) {
  652.         bc++;
  653.         frem = 0;
  654.         for (i = 0, p = &list->objects[0];
  655.              i < N_FLOATOBJECTS;
  656.              i++, p++) {
  657.             if (PyFloat_Check(p) && p->ob_refcnt != 0)
  658.                 frem++;
  659.         }
  660.         next = list->next;
  661.         if (frem) {
  662.             list->next = block_list;
  663.             block_list = list;
  664.             for (i = 0, p = &list->objects[0];
  665.                  i < N_FLOATOBJECTS;
  666.                  i++, p++) {
  667.                 if (!PyFloat_Check(p) || p->ob_refcnt == 0) {
  668.                     p->ob_type = (struct _typeobject *)
  669.                         free_list;
  670.                     free_list = p;
  671.                 }
  672.             }
  673.         }
  674.         else {
  675.             PyMem_FREE(list); /* XXX PyObject_FREE ??? */
  676.             bf++;
  677.         }
  678.         fsum += frem;
  679.         list = next;
  680.     }
  681.     if (!Py_VerboseFlag)
  682.         return;
  683.     fprintf(stderr, "# cleanup floats");
  684.     if (!fsum) {
  685.         fprintf(stderr, "\n");
  686.     }
  687.     else {
  688.         fprintf(stderr,
  689.             ": %d unfreed float%s in %d out of %d block%s\n",
  690.             fsum, fsum == 1 ? "" : "s",
  691.             bc - bf, bc, bc == 1 ? "" : "s");
  692.     }
  693.     if (Py_VerboseFlag > 1) {
  694.         list = block_list;
  695.         while (list != NULL) {
  696.             for (i = 0, p = &list->objects[0];
  697.                  i < N_FLOATOBJECTS;
  698.                  i++, p++) {
  699.                 if (PyFloat_Check(p) && p->ob_refcnt != 0) {
  700.                     char buf[100];
  701.                     PyFloat_AsString(buf, p);
  702.                     fprintf(stderr,
  703.                  "#   <float at %p, refcnt=%d, val=%s>\n",
  704.                         p, p->ob_refcnt, buf);
  705.                 }
  706.             }
  707.             list = list->next;
  708.         }
  709.     }
  710. }
  711.