home *** CD-ROM | disk | FTP | other *** search
/ linuxmafia.com 2016 / linuxmafia.com.tar / linuxmafia.com / pub / palmos / pippy-0.6beta-src.tar.gz / pippy-0.6beta-src.tar / pippy-0.6beta-src / src / Objects / floatobject.c < prev    next >
C/C++ Source or Header  |  2000-12-21  |  18KB  |  799 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* Float object implementation */
  33.  
  34. /* XXX There should be overflow checks here, but it's hard to check
  35.    for any kind of float exception without losing portability. */
  36.  
  37. #include "Python.h"
  38.  
  39. #ifndef WITHOUT_FLOAT
  40.  
  41. #include <ctype.h>
  42. #include "mymath.h"
  43. #include "other/floatobject_c.h"
  44.  
  45. #ifdef i860
  46. /* Cray APP has bogus definition of HUGE_VAL in <math.h> */
  47. #undef HUGE_VAL
  48. #endif
  49.  
  50. #if defined(HUGE_VAL) && !defined(CHECK)
  51. #define CHECK(x) if (errno != 0) ; \
  52.     else if (-HUGE_VAL <= (x) && (x) <= HUGE_VAL) ; \
  53.     else errno = ERANGE
  54. #endif
  55.  
  56. #ifndef CHECK
  57. #define CHECK(x) /* Don't know how to check */
  58. #endif
  59.  
  60. #ifdef HAVE_LIMITS_H
  61. #include <limits.h>
  62. #endif
  63.  
  64. #ifndef LONG_MAX
  65. #define LONG_MAX 0X7FFFFFFFL
  66. #endif
  67.  
  68. #ifndef LONG_MIN
  69. #define LONG_MIN (-LONG_MAX-1)
  70. #endif
  71.  
  72. #ifdef __NeXT__
  73. #ifdef __sparc__
  74. /*
  75.  * This works around a bug in the NS/Sparc 3.3 pre-release
  76.  * limits.h header file.
  77.  * 10-Feb-1995 bwarsaw@cnri.reston.va.us
  78.  */
  79. #undef LONG_MIN
  80. #define LONG_MIN (-LONG_MAX-1)
  81. #endif
  82. #endif
  83.  
  84. #if !defined(__STDC__) && !defined(macintosh)
  85. extern double fmod Py_PROTO((double, double));
  86. extern double pow Py_PROTO((double, double));
  87. #endif
  88.  
  89. #ifdef sun
  90. /* On SunOS4.1 only libm.a exists. Make sure that references to all
  91.    needed math functions exist in the executable, so that dynamic
  92.    loading of mathmodule does not fail. */
  93. double (*_Py_math_funcs_hack[])() = {
  94.     acos, asin, atan, atan2, ceil, cos, cosh, exp, fabs, floor,
  95.     fmod, log, log10, pow, sin, sinh, sqrt, tan, tanh
  96. };
  97. #endif
  98.  
  99. /* Special free list -- see comments for same code in intobject.c. */
  100. #define BLOCK_SIZE    1000    /* 1K less typical malloc overhead */
  101. #define BHEAD_SIZE    8    /* Enough for a 64-bit pointer */
  102. #define N_FLOATOBJECTS    ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyFloatObject))
  103.  
  104. #define PyMem_MALLOC    malloc
  105. #define PyMem_FREE    free
  106.  
  107. struct _floatblock {
  108.     struct _floatblock *next;
  109.     PyFloatObject objects[N_FLOATOBJECTS];
  110. };
  111.  
  112. typedef struct _floatblock PyFloatBlock;
  113.  
  114. static PyFloatBlock *block_list = NULL;
  115. static PyFloatObject *free_list = NULL;
  116.  
  117. static PyFloatObject *
  118. fill_free_list()
  119. {
  120.     PyFloatObject *p, *q;
  121.     p = (PyFloatObject *)PyMem_MALLOC(sizeof(PyFloatBlock));
  122.     if (p == NULL)
  123.         return (PyFloatObject *)PyErr_NoMemory();
  124.     ((PyFloatBlock *)p)->next = block_list;
  125.     block_list = (PyFloatBlock *)p;
  126.     p = &((PyFloatBlock *)p)->objects[0];
  127.     q = p + N_FLOATOBJECTS;
  128.     while (--q > p)
  129.         q->ob_type = (struct _typeobject *)(q-1);
  130.     q->ob_type = NULL;
  131.     return p + N_FLOATOBJECTS - 1;
  132. }
  133.  
  134. PyObject *
  135. #ifdef __SC__
  136. PyFloat_FromDouble(double fval)
  137. #else
  138. PyFloat_FromDouble(fval)
  139.     double fval;
  140. #endif
  141. {
  142.     register PyFloatObject *op;
  143.     if (free_list == NULL) {
  144.         if ((free_list = fill_free_list()) == NULL)
  145.             return NULL;
  146.     }
  147.     op = free_list;
  148.     free_list = (PyFloatObject *)op->ob_type;
  149.     op->ob_type = &PyFloat_Type;
  150.     op->ob_fval = fval;
  151.     _Py_NewReference((PyObject *)op);
  152.     return (PyObject *) op;
  153. }
  154.  
  155. PyObject *
  156. PyFloat_FromString(v, pend)
  157.     PyObject *v;
  158.     char **pend;
  159. {
  160.     extern double strtod Py_PROTO((const char *, char **));
  161.     char *s, *last, *end;
  162.     double x;
  163.     char buffer[256]; /* For errors */
  164.  
  165.     if (!PyString_Check(v))
  166.         return NULL;
  167.     s = PyString_AS_STRING(v);
  168.  
  169.     last = s + PyString_GET_SIZE(v);
  170.     while (*s && isspace(Py_CHARMASK(*s)))
  171.         s++;
  172.     if (s[0] == '\0') {
  173.         PyErr_SetString(PyExc_ValueError, "empty string for float()");
  174.         return NULL;
  175.     }
  176.     errno = 0;
  177.     PyFPE_START_PROTECT("PyFloat_FromString", return 0)
  178.     x = strtod(s, &end);
  179.     PyFPE_END_PROTECT(x)
  180.     /* Believe it or not, Solaris 2.6 can move end *beyond* the null
  181.        byte at the end of the string, when the input is inf(inity) */
  182.     if (end > last)
  183.         end = last;
  184.     while (*end && isspace(Py_CHARMASK(*end)))
  185.         end++;
  186.     if (*end != '\0') {
  187.         sprintf(buffer, "invalid literal for float(): %.200s", s);
  188.         PyErr_SetString(PyExc_ValueError, buffer);
  189.         return NULL;
  190.     }
  191.     else if (end != PyString_AS_STRING(v) + PyString_GET_SIZE(v)) {
  192.         PyErr_SetString(PyExc_ValueError,
  193.                 "null byte in argument for float()");
  194.         return NULL;
  195.     }
  196.     else if (errno != 0) {
  197.         sprintf(buffer, "float() literal too large: %.200s", s);
  198.         PyErr_SetString(PyExc_ValueError, buffer);
  199.         return NULL;
  200.     }
  201.     if (pend)
  202.         *pend = end;
  203.     return PyFloat_FromDouble(x);
  204. }
  205.  
  206. static void
  207. float_dealloc(op)
  208.     PyFloatObject *op;
  209. {
  210.     op->ob_type = (struct _typeobject *)free_list;
  211.     free_list = op;
  212. }
  213.  
  214. double
  215. PyFloat_AsDouble(op)
  216.     PyObject *op;
  217. {
  218.     PyNumberMethods *nb;
  219.     PyFloatObject *fo;
  220.     double val;
  221.     
  222.     if (op && PyFloat_Check(op))
  223.         return PyFloat_AS_DOUBLE((PyFloatObject*) op);
  224.     
  225.     if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
  226.         nb->nb_float == NULL) {
  227.         PyErr_BadArgument();
  228.         return -1;
  229.     }
  230.     
  231.     fo = (PyFloatObject*) (*nb->nb_float) (op);
  232.     if (fo == NULL)
  233.         return -1;
  234.     if (!PyFloat_Check(fo)) {
  235.         PyErr_SetString(PyExc_TypeError,
  236.                 "nb_float should return float object");
  237.         return -1;
  238.     }
  239.     
  240.     val = PyFloat_AS_DOUBLE(fo);
  241.     Py_DECREF(fo);
  242.     
  243.     return val;
  244. }
  245.  
  246. /* Methods */
  247.  
  248. void
  249. PyFloat_AsStringEx(buf, v, precision)
  250.     char *buf;
  251.     PyFloatObject *v;
  252.     int precision;
  253. {
  254.     register char *cp;
  255.     /* Subroutine for float_repr and float_print.
  256.        We want float numbers to be recognizable as such,
  257.        i.e., they should contain a decimal point or an exponent.
  258.        However, %g may print the number as an integer;
  259.        in such cases, we append ".0" to the string. */
  260.     sprintf(buf, "%.*g", precision, v->ob_fval);
  261.     cp = buf;
  262.     if (*cp == '-')
  263.         cp++;
  264.     for (; *cp != '\0'; cp++) {
  265.         /* Any non-digit means it's not an integer;
  266.            this takes care of NAN and INF as well. */
  267.         if (!isdigit(Py_CHARMASK(*cp)))
  268.             break;
  269.     }
  270.     if (*cp == '\0') {
  271.         *cp++ = '.';
  272.         *cp++ = '0';
  273.         *cp++ = '\0';
  274.     }
  275. }
  276.  
  277. /* Precisions used by repr() and str(), respectively.
  278.  
  279.    The repr() precision (17 significant decimal digits) is the minimal number
  280.    that is guaranteed to have enough precision so that if the number is read
  281.    back in the exact same binary value is recreated.  This is true for IEEE
  282.    floating point by design, and also happens to work for all other modern
  283.    hardware.
  284.  
  285.    The str() precision is chosen so that in most cases, the rounding noise
  286.    created by various operations is suppressed, while giving plenty of
  287.    precision for practical use.
  288.  
  289. */
  290.  
  291. #define PREC_REPR    17
  292. #define PREC_STR    12
  293.  
  294. void
  295. PyFloat_AsString(buf, v)
  296.     char *buf;
  297.     PyFloatObject *v;
  298. {
  299.     PyFloat_AsStringEx(buf, v, PREC_STR);
  300. }
  301.  
  302. /* ARGSUSED */
  303. static int
  304. float_print(v, fp, flags)
  305.     PyFloatObject *v;
  306.     FILE *fp;
  307.     int flags; /* Not used but required by interface */
  308. {
  309.     char buf[100];
  310.     PyFloat_AsStringEx(buf, v, flags&Py_PRINT_RAW ? PREC_STR : PREC_REPR);
  311.     fputs(buf, fp);
  312.     return 0;
  313. }
  314.  
  315. static PyObject *
  316. float_repr(v)
  317.     PyFloatObject *v;
  318. {
  319.     char buf[100];
  320.     PyFloat_AsStringEx(buf, v, PREC_REPR);
  321.     return PyString_FromString(buf);
  322. }
  323.  
  324. static PyObject *
  325. float_str(v)
  326.     PyFloatObject *v;
  327. {
  328.     char buf[100];
  329.     PyFloat_AsStringEx(buf, v, PREC_STR);
  330.     return PyString_FromString(buf);
  331. }
  332.  
  333. static int
  334. float_compare(v, w)
  335.     PyFloatObject *v, *w;
  336. {
  337.     double i = v->ob_fval;
  338.     double j = w->ob_fval;
  339.     return (i < j) ? -1 : (i > j) ? 1 : 0;
  340. }
  341.  
  342. static long
  343. float_hash(v)
  344.     PyFloatObject *v;
  345. {
  346.     double intpart, fractpart;
  347.     int expo;
  348.     long x;
  349.     /* This is designed so that Python numbers with the same
  350.        value hash to the same value, otherwise comparisons
  351.        of mapping keys will turn out weird */
  352.  
  353. #ifdef MPW /* MPW C modf expects pointer to extended as second argument */
  354. {
  355.     extended e;
  356.     fractpart = modf(v->ob_fval, &e);
  357.     intpart = e;
  358. }
  359. #else
  360.     fractpart = modf(v->ob_fval, &intpart);
  361. #endif
  362.  
  363.     if (fractpart == 0.0) {
  364.         if (intpart > 0x7fffffffL || -intpart > 0x7fffffffL) {
  365.             /* Convert to long int and use its hash... */
  366.             PyObject *w = PyLong_FromDouble(v->ob_fval);
  367.             if (w == NULL)
  368.                 return -1;
  369.             x = PyObject_Hash(w);
  370.             Py_DECREF(w);
  371.             return x;
  372.         }
  373.         x = (long)intpart;
  374.     }
  375.     else {
  376.         /* Note -- if you change this code, also change the copy
  377.            in complexobject.c */
  378.         long hipart;
  379.         fractpart = frexp(fractpart, &expo);
  380.         fractpart = fractpart * 2147483648.0; /* 2**31 */
  381.         hipart = (long)fractpart; /* Take the top 32 bits */
  382.         fractpart = (fractpart - (double)hipart) * 2147483648.0;
  383.                         /* Get the next 32 bits */
  384.         x = hipart + (long)fractpart + (long)intpart + (expo << 15);
  385.                         /* Combine everything */
  386.     }
  387.     if (x == -1)
  388.         x = -2;
  389.     return x;
  390. }
  391.  
  392. static PyObject *
  393. float_add(v, w)
  394.     PyFloatObject *v;
  395.     PyFloatObject *w;
  396. {
  397.     double result;
  398.     PyFPE_START_PROTECT("add", return 0)
  399.     result = v->ob_fval + w->ob_fval;
  400.     PyFPE_END_PROTECT(result)
  401.     return PyFloat_FromDouble(result);
  402. }
  403.  
  404. static PyObject *
  405. float_sub(v, w)
  406.     PyFloatObject *v;
  407.     PyFloatObject *w;
  408. {
  409.     double result;
  410.     PyFPE_START_PROTECT("subtract", return 0)
  411.     result = v->ob_fval - w->ob_fval;
  412.     PyFPE_END_PROTECT(result)
  413.     return PyFloat_FromDouble(result);
  414. }
  415.  
  416. static PyObject *
  417. float_mul(v, w)
  418.     PyFloatObject *v;
  419.     PyFloatObject *w;
  420. {
  421.     double result;
  422.  
  423.     PyFPE_START_PROTECT("multiply", return 0)
  424.     result = v->ob_fval * w->ob_fval;
  425.     PyFPE_END_PROTECT(result)
  426.     return PyFloat_FromDouble(result);
  427. }
  428.  
  429. static PyObject *
  430. float_div(v, w)
  431.     PyFloatObject *v;
  432.     PyFloatObject *w;
  433. {
  434.     double result;
  435.     if (w->ob_fval == 0) {
  436.         PyErr_SetString(PyExc_ZeroDivisionError, "float division");
  437.         return NULL;
  438.     }
  439.     PyFPE_START_PROTECT("divide", return 0)
  440.     result = v->ob_fval / w->ob_fval;
  441.     PyFPE_END_PROTECT(result)
  442.     return PyFloat_FromDouble(result);
  443. }
  444.  
  445. static PyObject *
  446. float_rem(v, w)
  447.     PyFloatObject *v;
  448.     PyFloatObject *w;
  449. {
  450.     double vx, wx;
  451.     double mod;
  452.     wx = w->ob_fval;
  453.     if (wx == 0.0) {
  454.         PyErr_SetString(PyExc_ZeroDivisionError, "float modulo");
  455.         return NULL;
  456.     }
  457.     PyFPE_START_PROTECT("modulo", return 0)
  458.     vx = v->ob_fval;
  459.     mod = fmod(vx, wx);
  460.     /* note: checking mod*wx < 0 is incorrect -- underflows to
  461.        0 if wx < sqrt(smallest nonzero double) */
  462.     if (mod && ((wx < 0) != (mod < 0))) {
  463.         mod += wx;
  464.     }
  465.     PyFPE_END_PROTECT(mod)
  466.     return PyFloat_FromDouble(mod);
  467. }
  468.  
  469. static PyObject *
  470. float_divmod(v, w)
  471.     PyFloatObject *v;
  472.     PyFloatObject *w;
  473. {
  474.     double vx, wx;
  475.     double div, mod, floordiv;
  476.     wx = w->ob_fval;
  477.     if (wx == 0.0) {
  478.         PyErr_SetString(PyExc_ZeroDivisionError, "float divmod()");
  479.         return NULL;
  480.     }
  481.     PyFPE_START_PROTECT("divmod", return 0)
  482.     vx = v->ob_fval;
  483.     mod = fmod(vx, wx);
  484.     /* fmod is typically exact, so vx-mod is *mathemtically* an
  485.        exact multiple of wx.  But this is fp arithmetic, and fp
  486.        vx - mod is an approximation; the result is that div may
  487.        not be an exact integral value after the division, although
  488.        it will always be very close to one.
  489.     */
  490.     div = (vx - mod) / wx;
  491.     /* note: checking mod*wx < 0 is incorrect -- underflows to
  492.        0 if wx < sqrt(smallest nonzero double) */
  493.     if (mod && ((wx < 0) != (mod < 0))) {
  494.         mod += wx;
  495.         div -= 1.0;
  496.     }
  497.     /* snap quotient to nearest integral value */
  498.     floordiv = floor(div);
  499.     if (div - floordiv > 0.5)
  500.         floordiv += 1.0;
  501.     PyFPE_END_PROTECT(div)
  502.     return Py_BuildValue("(dd)", floordiv, mod);
  503. }
  504.  
  505. static double powu(x, n)
  506.     double x;
  507.     long n;
  508. {
  509.     double r = 1.;
  510.     double p = x;
  511.     long mask = 1;
  512.     while (mask > 0 && n >= mask) {
  513.         if (n & mask)
  514.             r *= p;
  515.         mask <<= 1;
  516.         p *= p;
  517.     }
  518.     return r;
  519. }
  520.  
  521. static PyObject *
  522. float_pow(v, w, z)
  523.     PyFloatObject *v;
  524.     PyObject *w;
  525.     PyFloatObject *z;
  526. {
  527.     double iv, iw, ix;
  528.     long intw;
  529.  /* XXX Doesn't handle overflows if z!=None yet; it may never do so :(
  530.   * The z parameter is really only going to be useful for integers and
  531.   * long integers.  Maybe something clever with logarithms could be done.
  532.   * [AMK]
  533.   */
  534.     iv = v->ob_fval;
  535.     iw = ((PyFloatObject *)w)->ob_fval;
  536.     intw = (long)iw;
  537.     if (iw == intw && -10000 < intw && intw < 10000) {
  538.         /* Sort out special cases here instead of relying on pow() */
  539.         if (intw == 0) {         /* x**0 is 1, even 0**0 */
  540.             PyFPE_START_PROTECT("pow", return 0)
  541.              if ((PyObject *)z!=Py_None) {
  542.                  ix=fmod(1.0, z->ob_fval);
  543.                  if (ix!=0 && z->ob_fval<0) ix+=z->ob_fval;
  544.             }
  545.              else ix=1.0;
  546.             PyFPE_END_PROTECT(ix)
  547.                 return PyFloat_FromDouble(ix); 
  548.         }
  549.         errno = 0;
  550.         PyFPE_START_PROTECT("pow", return 0)
  551.         if (intw > 0)
  552.             ix = powu(iv, intw);
  553.         else
  554.             ix = 1./powu(iv, -intw);
  555.         PyFPE_END_PROTECT(ix)
  556.     }
  557.     else {
  558.         /* Sort out special cases here instead of relying on pow() */
  559.         if (iv == 0.0) {
  560.             if (iw < 0.0) {
  561.                 PyErr_SetString(PyExc_ValueError,
  562.                        "0.0 to a negative power");
  563.                 return NULL;
  564.             }
  565.             return PyFloat_FromDouble(0.0);
  566.         }
  567.         if (iv < 0.0) {
  568.             PyErr_SetString(PyExc_ValueError,
  569.                    "negative number to a float power");
  570.             return NULL;
  571.         }
  572.         errno = 0;
  573.         PyFPE_START_PROTECT("pow", return 0)
  574.         ix = pow(iv, iw);
  575.         PyFPE_END_PROTECT(ix)
  576.     }
  577.     CHECK(ix);
  578.     if (errno != 0) {
  579.         /* XXX could it be another type of error? */
  580.         PyErr_SetFromErrno(PyExc_OverflowError);
  581.         return NULL;
  582.     }
  583.      if ((PyObject *)z!=Py_None) {
  584.         PyFPE_START_PROTECT("pow", return 0)
  585.          ix=fmod(ix, z->ob_fval);    /* XXX To Be Rewritten */
  586.          if ( ix!=0 &&
  587.               ((iv<0 && z->ob_fval>0) || (iv>0 && z->ob_fval<0) )) {
  588.              ix+=z->ob_fval;
  589.             }
  590.         PyFPE_END_PROTECT(ix)
  591.     }
  592.     return PyFloat_FromDouble(ix);
  593. }
  594.  
  595. static PyObject *
  596. float_neg(v)
  597.     PyFloatObject *v;
  598. {
  599.     return PyFloat_FromDouble(-v->ob_fval);
  600. }
  601.  
  602. static PyObject *
  603. float_pos(v)
  604.     PyFloatObject *v;
  605. {
  606.     Py_INCREF(v);
  607.     return (PyObject *)v;
  608. }
  609.  
  610. static PyObject *
  611. float_abs(v)
  612.     PyFloatObject *v;
  613. {
  614.     if (v->ob_fval < 0)
  615.         return float_neg(v);
  616.     else
  617.         return float_pos(v);
  618. }
  619.  
  620. static int
  621. float_nonzero(v)
  622.     PyFloatObject *v;
  623. {
  624.     return v->ob_fval != 0.0;
  625. }
  626.  
  627. static int
  628. float_coerce(pv, pw)
  629.     PyObject **pv;
  630.     PyObject **pw;
  631. {
  632.     if (PyInt_Check(*pw)) {
  633.         long x = PyInt_AsLong(*pw);
  634.         *pw = PyFloat_FromDouble((double)x);
  635.         Py_INCREF(*pv);
  636.         return 0;
  637.     }
  638.     else if (PyLong_Check(*pw)) {
  639.         *pw = PyFloat_FromDouble(PyLong_AsDouble(*pw));
  640.         Py_INCREF(*pv);
  641.         return 0;
  642.     }
  643.     return 1; /* Can't do it */
  644. }
  645.  
  646. static PyObject *
  647. float_int(v)
  648.     PyObject *v;
  649. {
  650.     double x = PyFloat_AsDouble(v);
  651.     if (x < 0 ? (x = ceil(x)) < (double)LONG_MIN
  652.               : (x = floor(x)) > (double)LONG_MAX) {
  653.         PyErr_SetString(PyExc_OverflowError,
  654.                 "float too large to convert");
  655.         return NULL;
  656.     }
  657.     return PyInt_FromLong((long)x);
  658. }
  659.  
  660. static PyObject *
  661. float_long(v)
  662.     PyObject *v;
  663. {
  664.     double x = PyFloat_AsDouble(v);
  665.     return PyLong_FromDouble(x);
  666. }
  667.  
  668. static PyObject *
  669. float_float(v)
  670.     PyObject *v;
  671. {
  672.     Py_INCREF(v);
  673.     return v;
  674. }
  675.  
  676.  
  677. static PyNumberMethods float_as_number = {
  678.     (binaryfunc)float_add, /*nb_add*/
  679.     (binaryfunc)float_sub, /*nb_subtract*/
  680.     (binaryfunc)float_mul, /*nb_multiply*/
  681.     (binaryfunc)float_div, /*nb_divide*/
  682.     (binaryfunc)float_rem, /*nb_remainder*/
  683.     (binaryfunc)float_divmod, /*nb_divmod*/
  684.     (ternaryfunc)float_pow, /*nb_power*/
  685.     (unaryfunc)float_neg, /*nb_negative*/
  686.     (unaryfunc)float_pos, /*nb_positive*/
  687.     (unaryfunc)float_abs, /*nb_absolute*/
  688.     (inquiry)float_nonzero, /*nb_nonzero*/
  689.     0,        /*nb_invert*/
  690.     0,        /*nb_lshift*/
  691.     0,        /*nb_rshift*/
  692.     0,        /*nb_and*/
  693.     0,        /*nb_xor*/
  694.     0,        /*nb_or*/
  695.     (coercion)float_coerce, /*nb_coerce*/
  696.     (unaryfunc)float_int, /*nb_int*/
  697.     (unaryfunc)float_long, /*nb_long*/
  698.     (unaryfunc)float_float, /*nb_float*/
  699.     0,        /*nb_oct*/
  700.     0,        /*nb_hex*/
  701. };
  702.  
  703. PyTypeObject PyFloat_Type = {
  704.     PyObject_HEAD_INIT(&PyType_Type)
  705.     0,
  706.     "float",
  707.     sizeof(PyFloatObject),
  708.     0,
  709.     (destructor)float_dealloc, /*tp_dealloc*/
  710.     (printfunc)float_print, /*tp_print*/
  711.     0,            /*tp_getattr*/
  712.     0,            /*tp_setattr*/
  713.     (cmpfunc)float_compare, /*tp_compare*/
  714.     (reprfunc)float_repr,    /*tp_repr*/
  715.     &float_as_number,    /*tp_as_number*/
  716.     0,            /*tp_as_sequence*/
  717.     0,            /*tp_as_mapping*/
  718.     (hashfunc)float_hash,    /*tp_hash*/
  719.         0,            /*tp_call*/
  720.         (reprfunc)float_str,    /*tp_str*/
  721. };
  722.  
  723. void
  724. PyFloat_Fini()
  725. {
  726.     PyFloatObject *p;
  727.     PyFloatBlock *list, *next;
  728.     int i;
  729.     int bc, bf;    /* block count, number of freed blocks */
  730.     int frem, fsum;    /* remaining unfreed floats per block, total */
  731.  
  732.     bc = 0;
  733.     bf = 0;
  734.     fsum = 0;
  735.     list = block_list;
  736.     block_list = NULL;
  737.     free_list = NULL;
  738.     while (list != NULL) {
  739.         bc++;
  740.         frem = 0;
  741.         for (i = 0, p = &list->objects[0];
  742.              i < N_FLOATOBJECTS;
  743.              i++, p++) {
  744.             if (PyFloat_Check(p) && p->ob_refcnt != 0)
  745.                 frem++;
  746.         }
  747.         next = list->next;
  748.         if (frem) {
  749.             list->next = block_list;
  750.             block_list = list;
  751.             for (i = 0, p = &list->objects[0];
  752.                  i < N_FLOATOBJECTS;
  753.                  i++, p++) {
  754.                 if (!PyFloat_Check(p) || p->ob_refcnt == 0) {
  755.                     p->ob_type = (struct _typeobject *)
  756.                         free_list;
  757.                     free_list = p;
  758.                 }
  759.             }
  760.         }
  761.         else {
  762.             PyMem_FREE(list);
  763.             bf++;
  764.         }
  765.         fsum += frem;
  766.         list = next;
  767.     }
  768.     if (!Py_VerboseFlag)
  769.         return;
  770.     fprintf(stderr, "# cleanup floats");
  771.     if (!fsum) {
  772.         fprintf(stderr, "\n");
  773.     }
  774.     else {
  775.         fprintf(stderr,
  776.             ": %d unfreed float%s in %d out of %d block%s\n",
  777.             fsum, fsum == 1 ? "" : "s",
  778.             bc - bf, bc, bc == 1 ? "" : "s");
  779.     }
  780.     if (Py_VerboseFlag > 1) {
  781.         list = block_list;
  782.         while (list != NULL) {
  783.             for (i = 0, p = &list->objects[0];
  784.                  i < N_FLOATOBJECTS;
  785.                  i++, p++) {
  786.                 if (PyFloat_Check(p) && p->ob_refcnt != 0) {
  787.                     char buf[100];
  788.                     PyFloat_AsString(buf, p);
  789.                     fprintf(stderr,
  790.                  "#   <float at %lx, refcnt=%d, val=%s>\n",
  791.                         p, p->ob_refcnt, buf);
  792.                 }
  793.             }
  794.             list = list->next;
  795.         }
  796.     }
  797. }
  798. #endif /* WITHOUT_FLOAT */
  799.