home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Python 1.4 / Python 1.4 source / Objects / floatobject.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-28  |  10.8 KB  |  534 lines  |  [TEXT/CWIE]

  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 "allobjects.h"
  38. #include "modsupport.h"
  39.  
  40. #include <errno.h>
  41. #include <ctype.h>
  42. #include "mymath.h"
  43.  
  44. #ifdef i860
  45. /* Cray APP has bogus definition of HUGE_VAL in <math.h> */
  46. #undef HUGE_VAL
  47. #endif
  48.  
  49. #if defined(HUGE_VAL) && !defined(CHECK)
  50. #define CHECK(x) if (errno != 0) ; \
  51.     else if (-HUGE_VAL <= (x) && (x) <= HUGE_VAL) ; \
  52.     else errno = ERANGE
  53. #endif
  54.  
  55. #ifndef CHECK
  56. #define CHECK(x) /* Don't know how to check */
  57. #endif
  58.  
  59. #ifdef HAVE_LIMITS_H
  60. #include <limits.h>
  61. #endif
  62.  
  63. #ifndef LONG_MAX
  64. #define LONG_MAX 0X7FFFFFFFL
  65. #endif
  66.  
  67. #ifndef LONG_MIN
  68. #define LONG_MIN (-LONG_MAX-1)
  69. #endif
  70.  
  71. #ifdef __NeXT__
  72. #ifdef __sparc__
  73. /*
  74.  * This works around a bug in the NS/Sparc 3.3 pre-release
  75.  * limits.h header file.
  76.  * 10-Feb-1995 bwarsaw@cnri.reston.va.us
  77.  */
  78. #undef LONG_MIN
  79. #define LONG_MIN (-LONG_MAX-1)
  80. #endif
  81. #endif
  82.  
  83. #if !defined(__STDC__) && !defined(macintosh)
  84. extern double fmod PROTO((double, double));
  85. extern double pow PROTO((double, double));
  86. #endif
  87.  
  88. object *
  89. #ifdef __SC__
  90. newfloatobject(double fval)
  91. #else
  92. newfloatobject(fval)
  93.     double fval;
  94. #endif
  95. {
  96.     /* For efficiency, this code is copied from newobject() */
  97.     register floatobject *op = (floatobject *) malloc(sizeof(floatobject));
  98.     if (op == NULL)
  99.         return err_nomem();
  100.     op->ob_type = &Floattype;
  101.     op->ob_fval = fval;
  102.     NEWREF(op);
  103.     return (object *) op;
  104. }
  105.  
  106. static void
  107. float_dealloc(op)
  108.     object *op;
  109. {
  110.     DEL(op);
  111. }
  112.  
  113. double
  114. getfloatvalue(op)
  115.     object *op;
  116. {
  117.     number_methods *nb;
  118.     floatobject *fo;
  119.     double val;
  120.     
  121.     if (op && is_floatobject(op))
  122.         return GETFLOATVALUE((floatobject*) op);
  123.     
  124.     if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
  125.         nb->nb_float == NULL) {
  126.         err_badarg();
  127.         return -1;
  128.     }
  129.     
  130.     fo = (floatobject*) (*nb->nb_float) (op);
  131.     if (fo == NULL)
  132.         return -1;
  133.     if (!is_floatobject(fo)) {
  134.         err_setstr(TypeError, "nb_float should return float object");
  135.         return -1;
  136.     }
  137.     
  138.     val = GETFLOATVALUE(fo);
  139.     DECREF(fo);
  140.     
  141.     return val;
  142. }
  143.  
  144. /* Methods */
  145.  
  146. void
  147. float_buf_repr(buf, v)
  148.     char *buf;
  149.     floatobject *v;
  150. {
  151.     register char *cp;
  152.     /* Subroutine for float_repr and float_print.
  153.        We want float numbers to be recognizable as such,
  154.        i.e., they should contain a decimal point or an exponent.
  155.        However, %g may print the number as an integer;
  156.        in such cases, we append ".0" to the string. */
  157.     sprintf(buf, "%.12g", v->ob_fval);
  158.     cp = buf;
  159.     if (*cp == '-')
  160.         cp++;
  161.     for (; *cp != '\0'; cp++) {
  162.         /* Any non-digit means it's not an integer;
  163.            this takes care of NAN and INF as well. */
  164.         if (!isdigit(Py_CHARMASK(*cp)))
  165.             break;
  166.     }
  167.     if (*cp == '\0') {
  168.         *cp++ = '.';
  169.         *cp++ = '0';
  170.         *cp++ = '\0';
  171.     }
  172. }
  173.  
  174. /* ARGSUSED */
  175. static int
  176. float_print(v, fp, flags)
  177.     floatobject *v;
  178.     FILE *fp;
  179.     int flags; /* Not used but required by interface */
  180. {
  181.     char buf[100];
  182.     float_buf_repr(buf, v);
  183.     fputs(buf, fp);
  184.     return 0;
  185. }
  186.  
  187. static object *
  188. float_repr(v)
  189.     floatobject *v;
  190. {
  191.     char buf[100];
  192.     float_buf_repr(buf, v);
  193.     return newstringobject(buf);
  194. }
  195.  
  196. static int
  197. float_compare(v, w)
  198.     floatobject *v, *w;
  199. {
  200.     double i = v->ob_fval;
  201.     double j = w->ob_fval;
  202.     return (i < j) ? -1 : (i > j) ? 1 : 0;
  203. }
  204.  
  205. static long
  206. float_hash(v)
  207.     floatobject *v;
  208. {
  209.     double intpart, fractpart;
  210.     int expo;
  211.     long x;
  212.     /* This is designed so that Python numbers with the same
  213.        value hash to the same value, otherwise comparisons
  214.        of mapping keys will turn out weird */
  215.  
  216. #ifdef MPW /* MPW C modf expects pointer to extended as second argument */
  217. {
  218.     extended e;
  219.     fractpart = modf(v->ob_fval, &e);
  220.     intpart = e;
  221. }
  222. #else
  223.     fractpart = modf(v->ob_fval, &intpart);
  224. #endif
  225.  
  226.     if (fractpart == 0.0) {
  227.         if (intpart > 0x7fffffffL || -intpart > 0x7fffffffL) {
  228.             /* Convert to long int and use its hash... */
  229.             object *w = dnewlongobject(v->ob_fval);
  230.             if (w == NULL)
  231.                 return -1;
  232.             x = hashobject(w);
  233.             DECREF(w);
  234.             return x;
  235.         }
  236.         x = (long)intpart;
  237.     }
  238.     else {
  239.         fractpart = frexp(fractpart, &expo);
  240.         fractpart = fractpart*2147483648.0; /* 2**31 */
  241.         x = (long) (intpart + fractpart) ^ expo; /* Rather arbitrary */
  242.     }
  243.     if (x == -1)
  244.         x = -2;
  245.     return x;
  246. }
  247.  
  248. static object *
  249. float_add(v, w)
  250.     floatobject *v;
  251.     floatobject *w;
  252. {
  253.     return newfloatobject(v->ob_fval + w->ob_fval);
  254. }
  255.  
  256. static object *
  257. float_sub(v, w)
  258.     floatobject *v;
  259.     floatobject *w;
  260. {
  261.     return newfloatobject(v->ob_fval - w->ob_fval);
  262. }
  263.  
  264. static object *
  265. float_mul(v, w)
  266.     floatobject *v;
  267.     floatobject *w;
  268. {
  269.     return newfloatobject(v->ob_fval * w->ob_fval);
  270. }
  271.  
  272. static object *
  273. float_div(v, w)
  274.     floatobject *v;
  275.     floatobject *w;
  276. {
  277.     if (w->ob_fval == 0) {
  278.         err_setstr(ZeroDivisionError, "float division");
  279.         return NULL;
  280.     }
  281.     return newfloatobject(v->ob_fval / w->ob_fval);
  282. }
  283.  
  284. static object *
  285. float_rem(v, w)
  286.     floatobject *v;
  287.     floatobject *w;
  288. {
  289.     double vx, wx;
  290.     double /* div, */ mod;
  291.     wx = w->ob_fval;
  292.     if (wx == 0.0) {
  293.         err_setstr(ZeroDivisionError, "float modulo");
  294.         return NULL;
  295.     }
  296.     vx = v->ob_fval;
  297.     mod = fmod(vx, wx);
  298.     /* div = (vx - mod) / wx; */
  299.     if (wx*mod < 0) {
  300.         mod += wx;
  301.         /* div -= 1.0; */
  302.     }
  303.     return newfloatobject(mod);
  304. }
  305.  
  306. static object *
  307. float_divmod(v, w)
  308.     floatobject *v;
  309.     floatobject *w;
  310. {
  311.     double vx, wx;
  312.     double div, mod;
  313.     wx = w->ob_fval;
  314.     if (wx == 0.0) {
  315.         err_setstr(ZeroDivisionError, "float divmod()");
  316.         return NULL;
  317.     }
  318.     vx = v->ob_fval;
  319.     mod = fmod(vx, wx);
  320.     div = (vx - mod) / wx;
  321.     if (wx*mod < 0) {
  322.         mod += wx;
  323.         div -= 1.0;
  324.     }
  325.     return mkvalue("(dd)", div, mod);
  326. }
  327.  
  328. static double powu(x, n)
  329.     double x;
  330.     long n;
  331. {
  332.     double r = 1.;
  333.     double p = x;
  334.     long mask = 1;
  335.     while (mask > 0 && n >= mask) {
  336.         if (n & mask)
  337.             r *= p;
  338.         mask <<= 1;
  339.         p *= p;
  340.     }
  341.     return r;
  342. }
  343.  
  344. static object *
  345. float_pow(v, w, z)
  346.     floatobject *v;
  347.     object *w;
  348.     floatobject *z;
  349. {
  350.     double iv, iw, ix;
  351.     long intw;
  352.  /* XXX Doesn't handle overflows if z!=None yet; it may never do so :(
  353.   * The z parameter is really only going to be useful for integers and
  354.   * long integers.  Maybe something clever with logarithms could be done.
  355.   * [AMK]
  356.   */
  357.     iv = v->ob_fval;
  358.     iw = ((floatobject *)w)->ob_fval;
  359.     intw = (long)iw;
  360.     if (iw == intw && -10000 < intw && intw < 10000) {
  361.         /* Sort out special cases here instead of relying on pow() */
  362.         if (intw == 0) {         /* x**0 is 1, even 0**0 */
  363.              if ((object *)z!=None) {
  364.                  ix=fmod(1.0, z->ob_fval);
  365.                  if (ix!=0 && z->ob_fval<0) ix+=z->ob_fval;
  366.             }
  367.              else ix=1.0;
  368.                 return newfloatobject(ix); 
  369.         }
  370.         errno = 0;
  371.         if (intw > 0)
  372.             ix = powu(iv, intw);
  373.         else
  374.             ix = 1./powu(iv, -intw);
  375.     }
  376.     else {
  377.         /* Sort out special cases here instead of relying on pow() */
  378.         if (iv == 0.0) {
  379.             if (iw < 0.0) {
  380.                 err_setstr(ValueError,
  381.                        "0.0 to a negative power");
  382.                 return NULL;
  383.             }
  384.             return newfloatobject(0.0);
  385.         }
  386.         if (iv < 0.0) {
  387.             err_setstr(ValueError,
  388.                    "negative number to a float power");
  389.             return NULL;
  390.         }
  391.         errno = 0;
  392.         ix = pow(iv, iw);
  393.     }
  394.     CHECK(ix);
  395.     if (errno != 0) {
  396.         /* XXX could it be another type of error? */
  397.         err_errno(OverflowError);
  398.         return NULL;
  399.     }
  400.      if ((object *)z!=None) {
  401.          ix=fmod(ix, z->ob_fval);    /* XXX To Be Rewritten */
  402.          if ( ix!=0 &&
  403.               ((iv<0 && z->ob_fval>0) || (iv>0 && z->ob_fval<0) )) {
  404.              ix+=z->ob_fval;
  405.             }
  406.     }
  407.     return newfloatobject(ix);
  408. }
  409.  
  410. static object *
  411. float_neg(v)
  412.     floatobject *v;
  413. {
  414.     return newfloatobject(-v->ob_fval);
  415. }
  416.  
  417. static object *
  418. float_pos(v)
  419.     floatobject *v;
  420. {
  421.     INCREF(v);
  422.     return (object *)v;
  423. }
  424.  
  425. static object *
  426. float_abs(v)
  427.     floatobject *v;
  428. {
  429.     if (v->ob_fval < 0)
  430.         return float_neg(v);
  431.     else
  432.         return float_pos(v);
  433. }
  434.  
  435. static int
  436. float_nonzero(v)
  437.     floatobject *v;
  438. {
  439.     return v->ob_fval != 0.0;
  440. }
  441.  
  442. static int
  443. float_coerce(pv, pw)
  444.     object **pv;
  445.     object **pw;
  446. {
  447.     if (is_intobject(*pw)) {
  448.         long x = getintvalue(*pw);
  449.         *pw = newfloatobject((double)x);
  450.         INCREF(*pv);
  451.         return 0;
  452.     }
  453.     else if (is_longobject(*pw)) {
  454.         *pw = newfloatobject(dgetlongvalue(*pw));
  455.         INCREF(*pv);
  456.         return 0;
  457.     }
  458.     return 1; /* Can't do it */
  459. }
  460.  
  461. static object *
  462. float_int(v)
  463.     object *v;
  464. {
  465.     double x = getfloatvalue(v);
  466.     if (x < 0 ? (x = ceil(x)) < (double)LONG_MIN
  467.               : (x = floor(x)) > (double)LONG_MAX) {
  468.         err_setstr(OverflowError, "float too large to convert");
  469.         return NULL;
  470.     }
  471.     return newintobject((long)x);
  472. }
  473.  
  474. static object *
  475. float_long(v)
  476.     object *v;
  477. {
  478.     double x = getfloatvalue(v);
  479.     return dnewlongobject(x);
  480. }
  481.  
  482. static object *
  483. float_float(v)
  484.     object *v;
  485. {
  486.     INCREF(v);
  487.     return v;
  488. }
  489.  
  490.  
  491. static number_methods float_as_number = {
  492.     (binaryfunc)float_add, /*nb_add*/
  493.     (binaryfunc)float_sub, /*nb_subtract*/
  494.     (binaryfunc)float_mul, /*nb_multiply*/
  495.     (binaryfunc)float_div, /*nb_divide*/
  496.     (binaryfunc)float_rem, /*nb_remainder*/
  497.     (binaryfunc)float_divmod, /*nb_divmod*/
  498.     (ternaryfunc)float_pow, /*nb_power*/
  499.     (unaryfunc)float_neg, /*nb_negative*/
  500.     (unaryfunc)float_pos, /*nb_positive*/
  501.     (unaryfunc)float_abs, /*nb_absolute*/
  502.     (inquiry)float_nonzero, /*nb_nonzero*/
  503.     0,        /*nb_invert*/
  504.     0,        /*nb_lshift*/
  505.     0,        /*nb_rshift*/
  506.     0,        /*nb_and*/
  507.     0,        /*nb_xor*/
  508.     0,        /*nb_or*/
  509.     (coercion)float_coerce, /*nb_coerce*/
  510.     (unaryfunc)float_int, /*nb_int*/
  511.     (unaryfunc)float_long, /*nb_long*/
  512.     (unaryfunc)float_float, /*nb_float*/
  513.     0,        /*nb_oct*/
  514.     0,        /*nb_hex*/
  515. };
  516.  
  517. typeobject Floattype = {
  518.     OB_HEAD_INIT(&Typetype)
  519.     0,
  520.     "float",
  521.     sizeof(floatobject),
  522.     0,
  523.     (destructor)float_dealloc, /*tp_dealloc*/
  524.     (printfunc)float_print, /*tp_print*/
  525.     0,            /*tp_getattr*/
  526.     0,            /*tp_setattr*/
  527.     (cmpfunc)float_compare, /*tp_compare*/
  528.     (reprfunc)float_repr, /*tp_repr*/
  529.     &float_as_number,    /*tp_as_number*/
  530.     0,            /*tp_as_sequence*/
  531.     0,            /*tp_as_mapping*/
  532.     (hashfunc)float_hash, /*tp_hash*/
  533. };
  534.