home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyth_os2.zip / python-1.0.2 / Objects / floatobject.c < prev    next >
C/C++ Source or Header  |  1994-01-04  |  9KB  |  417 lines

  1. /***********************************************************
  2. Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
  3. Amsterdam, 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 not be used in advertising or publicity pertaining to
  13. distribution of the software without specific, written prior permission.
  14.  
  15. STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  16. THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  18. FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  20. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  21. OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  
  23. ******************************************************************/
  24.  
  25. /* Float object implementation */
  26.  
  27. /* XXX There should be overflow checks here, but it's hard to check
  28.    for any kind of float exception without losing portability. */
  29.  
  30. #include "allobjects.h"
  31. #include "modsupport.h"
  32.  
  33. #include <errno.h>
  34. #include <ctype.h>
  35. #include <math.h>
  36.  
  37. #ifdef i860
  38. /* Cray APP has bogus definition of HUGE_VAL in <math.h> */
  39. #undef HUGE_VAL
  40. #endif
  41.  
  42. #ifdef HUGE_VAL
  43. #define CHECK(x) if (errno != 0) ; \
  44.     else if (-HUGE_VAL <= (x) && (x) <= HUGE_VAL) ; \
  45.     else errno = ERANGE
  46. #else
  47. #define CHECK(x) /* Don't know how to check */
  48. #endif
  49.  
  50. #ifndef THINK_C
  51. extern double fmod PROTO((double, double));
  52. extern double pow PROTO((double, double));
  53. #endif
  54.  
  55. object *
  56. newfloatobject(fval)
  57.     double fval;
  58. {
  59.     /* For efficiency, this code is copied from newobject() */
  60.     register floatobject *op = (floatobject *) malloc(sizeof(floatobject));
  61.     if (op == NULL)
  62.         return err_nomem();
  63.     op->ob_type = &Floattype;
  64.     op->ob_fval = fval;
  65.     NEWREF(op);
  66.     return (object *) op;
  67. }
  68.  
  69. static void
  70. float_dealloc(op)
  71.     object *op;
  72. {
  73.     DEL(op);
  74. }
  75.  
  76. double
  77. getfloatvalue(op)
  78.     object *op;
  79. {
  80.     if (!is_floatobject(op)) {
  81.         err_badarg();
  82.         return -1;
  83.     }
  84.     else
  85.         return ((floatobject *)op) -> ob_fval;
  86. }
  87.  
  88. /* Methods */
  89.  
  90. void
  91. float_buf_repr(buf, v)
  92.     char *buf;
  93.     floatobject *v;
  94. {
  95.     register char *cp;
  96.     /* Subroutine for float_repr and float_print.
  97.        We want float numbers to be recognizable as such,
  98.        i.e., they should contain a decimal point or an exponent.
  99.        However, %g may print the number as an integer;
  100.        in such cases, we append ".0" to the string. */
  101.     sprintf(buf, "%.12g", v->ob_fval);
  102.     cp = buf;
  103.     if (*cp == '-')
  104.         cp++;
  105.     for (; *cp != '\0'; cp++) {
  106.         /* Any non-digit means it's not an integer;
  107.            this takes care of NAN and INF as well. */
  108.         if (!isdigit(*cp))
  109.             break;
  110.     }
  111.     if (*cp == '\0') {
  112.         *cp++ = '.';
  113.         *cp++ = '0';
  114.         *cp++ = '\0';
  115.     }
  116. }
  117.  
  118. /* ARGSUSED */
  119. static int
  120. float_print(v, fp, flags)
  121.     floatobject *v;
  122.     FILE *fp;
  123.     int flags; /* Not used but required by interface */
  124. {
  125.     char buf[100];
  126.     float_buf_repr(buf, v);
  127.     fputs(buf, fp);
  128.     return 0;
  129. }
  130.  
  131. static object *
  132. float_repr(v)
  133.     floatobject *v;
  134. {
  135.     char buf[100];
  136.     float_buf_repr(buf, v);
  137.     return newstringobject(buf);
  138. }
  139.  
  140. static int
  141. float_compare(v, w)
  142.     floatobject *v, *w;
  143. {
  144.     double i = v->ob_fval;
  145.     double j = w->ob_fval;
  146.     return (i < j) ? -1 : (i > j) ? 1 : 0;
  147. }
  148.  
  149. static long
  150. float_hash(v)
  151.     floatobject *v;
  152. {
  153.     double intpart, fractpart;
  154.     int expo;
  155.     long x;
  156.     /* This is designed so that Python numbers with the same
  157.        value hash to the same value, otherwise comparisons
  158.        of mapping keys will turn out weird */
  159.     fractpart = modf(v->ob_fval, &intpart);
  160.     if (fractpart == 0.0) {
  161.         if (intpart > 0x7fffffffL || -intpart > 0x7fffffffL) {
  162.             /* Convert to long int and use its hash... */
  163.             object *w = dnewlongobject(v->ob_fval);
  164.             if (w == NULL)
  165.                 return -1;
  166.             x = hashobject(w);
  167.             DECREF(w);
  168.             return x;
  169.         }
  170.         x = (long)intpart;
  171.     }
  172.     else {
  173.         fractpart = frexp(fractpart, &expo);
  174.         fractpart = fractpart*4294967296.0; /* 2**32 */
  175.         x = (long) (intpart + fractpart) ^ expo; /* Rather arbitrary */
  176.     }
  177.     if (x == -1)
  178.         x = -2;
  179.     return x;
  180. }
  181.  
  182. static object *
  183. float_add(v, w)
  184.     floatobject *v;
  185.     floatobject *w;
  186. {
  187.     return newfloatobject(v->ob_fval + w->ob_fval);
  188. }
  189.  
  190. static object *
  191. float_sub(v, w)
  192.     floatobject *v;
  193.     floatobject *w;
  194. {
  195.     return newfloatobject(v->ob_fval - w->ob_fval);
  196. }
  197.  
  198. static object *
  199. float_mul(v, w)
  200.     floatobject *v;
  201.     floatobject *w;
  202. {
  203.     return newfloatobject(v->ob_fval * w->ob_fval);
  204. }
  205.  
  206. static object *
  207. float_div(v, w)
  208.     floatobject *v;
  209.     floatobject *w;
  210. {
  211.     if (w->ob_fval == 0) {
  212.         err_setstr(ZeroDivisionError, "float division");
  213.         return NULL;
  214.     }
  215.     return newfloatobject(v->ob_fval / w->ob_fval);
  216. }
  217.  
  218. static object *
  219. float_rem(v, w)
  220.     floatobject *v;
  221.     floatobject *w;
  222. {
  223.     double vx, wx;
  224.     double /* div, */ mod;
  225.     wx = w->ob_fval;
  226.     if (wx == 0.0) {
  227.         err_setstr(ZeroDivisionError, "float modulo");
  228.         return NULL;
  229.     }
  230.     vx = v->ob_fval;
  231.     mod = fmod(vx, wx);
  232.     /* div = (vx - mod) / wx; */
  233.     if (wx*mod < 0) {
  234.         mod += wx;
  235.         /* div -= 1.0; */
  236.     }
  237.     return newfloatobject(mod);
  238. }
  239.  
  240. static object *
  241. float_divmod(v, w)
  242.     floatobject *v;
  243.     floatobject *w;
  244. {
  245.     double vx, wx;
  246.     double div, mod;
  247.     object *t;
  248.     wx = w->ob_fval;
  249.     if (wx == 0.0) {
  250.         err_setstr(ZeroDivisionError, "float divmod()");
  251.         return NULL;
  252.     }
  253.     vx = v->ob_fval;
  254.     mod = fmod(vx, wx);
  255.     div = (vx - mod) / wx;
  256.     if (wx*mod < 0) {
  257.         mod += wx;
  258.         div -= 1.0;
  259.     }
  260.     return mkvalue("(dd)", div, mod);
  261. }
  262.  
  263. static object *
  264. float_pow(v, w)
  265.     floatobject *v;
  266.     floatobject *w;
  267. {
  268.     double iv, iw, ix;
  269.     iv = v->ob_fval;
  270.     iw = w->ob_fval;
  271.     /* Sort out special cases here instead of relying on pow() */
  272.     if (iw == 0.0)
  273.         return newfloatobject(1.0); /* x**0 is 1, even 0**0 */
  274.     if (iv == 0.0) {
  275.         if (iw < 0.0) {
  276.             err_setstr(ValueError, "0.0 to the negative power");
  277.             return NULL;
  278.         }
  279.         return newfloatobject(0.0);
  280.     }
  281.     if (iv < 0.0) {
  282.         err_setstr(ValueError, "negative float to float power");
  283.         return NULL;
  284.     }
  285.     errno = 0;
  286.     ix = pow(iv, iw);
  287.     CHECK(ix);
  288.     if (errno != 0) {
  289.         /* XXX could it be another type of error? */
  290.         err_errno(OverflowError);
  291.         return NULL;
  292.     }
  293.     return newfloatobject(ix);
  294. }
  295.  
  296. static object *
  297. float_neg(v)
  298.     floatobject *v;
  299. {
  300.     return newfloatobject(-v->ob_fval);
  301. }
  302.  
  303. static object *
  304. float_pos(v)
  305.     floatobject *v;
  306. {
  307.     INCREF(v);
  308.     return (object *)v;
  309. }
  310.  
  311. static object *
  312. float_abs(v)
  313.     floatobject *v;
  314. {
  315.     if (v->ob_fval < 0)
  316.         return float_neg(v);
  317.     else
  318.         return float_pos(v);
  319. }
  320.  
  321. static int
  322. float_nonzero(v)
  323.     floatobject *v;
  324. {
  325.     return v->ob_fval != 0.0;
  326. }
  327.  
  328. static int
  329. float_coerce(pv, pw)
  330.     object **pv;
  331.     object **pw;
  332. {
  333.     if (is_intobject(*pw)) {
  334.         long x = getintvalue(*pw);
  335.         *pw = newfloatobject((double)x);
  336.         INCREF(*pv);
  337.         return 0;
  338.     }
  339.     else if (is_longobject(*pw)) {
  340.         *pw = newfloatobject(dgetlongvalue(*pw));
  341.         INCREF(*pv);
  342.         return 0;
  343.     }
  344.     return 1; /* Can't do it */
  345. }
  346.  
  347. static object *
  348. float_int(v)
  349.     object *v;
  350. {
  351.     double x = getfloatvalue(v);
  352.     /* XXX should check for overflow */
  353.     /* XXX should define how we round */
  354.     return newintobject((long)x);
  355. }
  356.  
  357. static object *
  358. float_long(v)
  359.     object *v;
  360. {
  361.     double x = getfloatvalue(v);
  362.     return dnewlongobject(x);
  363. }
  364.  
  365. static object *
  366. float_float(v)
  367.     object *v;
  368. {
  369.     INCREF(v);
  370.     return v;
  371. }
  372.  
  373.  
  374. static number_methods float_as_number = {
  375.     (binaryfunc)float_add, /*nb_add*/
  376.     (binaryfunc)float_sub, /*nb_subtract*/
  377.     (binaryfunc)float_mul, /*nb_multiply*/
  378.     (binaryfunc)float_div, /*nb_divide*/
  379.     (binaryfunc)float_rem, /*nb_remainder*/
  380.     (binaryfunc)float_divmod, /*nb_divmod*/
  381.     (binaryfunc)float_pow, /*nb_power*/
  382.     (unaryfunc)float_neg, /*nb_negative*/
  383.     (unaryfunc)float_pos, /*nb_positive*/
  384.     (unaryfunc)float_abs, /*nb_absolute*/
  385.     (inquiry)float_nonzero, /*nb_nonzero*/
  386.     0,        /*nb_invert*/
  387.     0,        /*nb_lshift*/
  388.     0,        /*nb_rshift*/
  389.     0,        /*nb_and*/
  390.     0,        /*nb_xor*/
  391.     0,        /*nb_or*/
  392.     (coercion)float_coerce, /*nb_coerce*/
  393.     (unaryfunc)float_int, /*nb_int*/
  394.     (unaryfunc)float_long, /*nb_long*/
  395.     (unaryfunc)float_float, /*nb_float*/
  396.     0,        /*nb_oct*/
  397.     0,        /*nb_hex*/
  398. };
  399.  
  400. typeobject Floattype = {
  401.     OB_HEAD_INIT(&Typetype)
  402.     0,
  403.     "float",
  404.     sizeof(floatobject),
  405.     0,
  406.     (destructor)float_dealloc, /*tp_dealloc*/
  407.     (printfunc)float_print, /*tp_print*/
  408.     0,            /*tp_getattr*/
  409.     0,            /*tp_setattr*/
  410.     (cmpfunc)float_compare, /*tp_compare*/
  411.     (reprfunc)float_repr, /*tp_repr*/
  412.     &float_as_number,    /*tp_as_number*/
  413.     0,            /*tp_as_sequence*/
  414.     0,            /*tp_as_mapping*/
  415.     (hashfunc)float_hash, /*tp_hash*/
  416. };
  417.