home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyth_os2.zip / python-1.0.2 / Modules / mathmodule.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-02  |  5.0 KB  |  228 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. /* Math module -- standard C math library functions, pi and e */
  26.  
  27. #include "allobjects.h"
  28.  
  29. #include <errno.h>
  30.  
  31. #include "modsupport.h"
  32.  
  33. #define getdoublearg(v, a) getargs(v, "d", a)
  34. #define get2doublearg(v, a, b) getargs(v, "(dd)", a, b)
  35.  
  36. #include <math.h>
  37.  
  38. #ifdef i860
  39. /* Cray APP has bogus definition of HUGE_VAL in <math.h> */
  40. #undef HUGE_VAL
  41. #endif
  42.  
  43. #ifndef __STDC__
  44. extern double fmod PROTO((double, double));
  45. #endif
  46.  
  47. #ifdef HUGE_VAL
  48. #define CHECK(x) if (errno != 0) ; \
  49.     else if (-HUGE_VAL <= (x) && (x) <= HUGE_VAL) ; \
  50.     else errno = ERANGE
  51. #else
  52. #define CHECK(x) /* Don't know how to check */
  53. #endif
  54.  
  55. static object *
  56. math_error()
  57. {
  58.     if (errno == EDOM)
  59.         err_setstr(ValueError, "math domain error");
  60.     else if (errno == ERANGE)
  61.         err_setstr(OverflowError, "math range error");
  62.     else
  63.         err_errno(ValueError); /* Unexpected math error */
  64.     return NULL;
  65. }
  66.  
  67. static object *
  68. math_1(args, func)
  69.     object *args;
  70.     double (*func) FPROTO((double));
  71. {
  72.     double x;
  73.     if (!getdoublearg(args, &x))
  74.         return NULL;
  75.     errno = 0;
  76.     x = (*func)(x);
  77.     CHECK(x);
  78.     if (errno != 0)
  79.         return math_error();
  80.     else
  81.         return newfloatobject(x);
  82. }
  83.  
  84. static object *
  85. math_2(args, func)
  86.     object *args;
  87.     double (*func) FPROTO((double, double));
  88. {
  89.     double x, y;
  90.     if (!get2doublearg(args, &x, &y))
  91.         return NULL;
  92.     errno = 0;
  93.     x = (*func)(x, y);
  94.     CHECK(x);
  95.     if (errno != 0)
  96.         return math_error();
  97.     else
  98.         return newfloatobject(x);
  99. }
  100.  
  101. #define FUNC1(stubname, func) \
  102.     static object * stubname(self, args) object *self, *args; { \
  103.         return math_1(args, func); \
  104.     }
  105.  
  106. #define FUNC2(stubname, func) \
  107.     static object * stubname(self, args) object *self, *args; { \
  108.         return math_2(args, func); \
  109.     }
  110.  
  111. FUNC1(math_acos, acos)
  112. FUNC1(math_asin, asin)
  113. FUNC1(math_atan, atan)
  114. FUNC2(math_atan2, atan2)
  115. FUNC1(math_ceil, ceil)
  116. FUNC1(math_cos, cos)
  117. FUNC1(math_cosh, cosh)
  118. FUNC1(math_exp, exp)
  119. FUNC1(math_fabs, fabs)
  120. FUNC1(math_floor, floor)
  121. FUNC2(math_fmod, fmod)
  122. FUNC1(math_log, log)
  123. FUNC1(math_log10, log10)
  124. #ifdef MPW_3_1 /* This hack is needed for MPW 3.1 but not for 3.2 ... */
  125. FUNC2(math_pow, power)
  126. #else
  127. FUNC2(math_pow, pow)
  128. #endif
  129. FUNC1(math_sin, sin)
  130. FUNC1(math_sinh, sinh)
  131. FUNC1(math_sqrt, sqrt)
  132. FUNC1(math_tan, tan)
  133. FUNC1(math_tanh, tanh)
  134.  
  135. double    frexp PROTO((double, int *));
  136. double    ldexp PROTO((double, int));
  137. double    modf PROTO((double, double *));
  138.  
  139. static object *
  140. math_frexp(self, args)
  141.     object *self;
  142.     object *args;
  143. {
  144.     double x;
  145.     int i;
  146.     if (!getdoublearg(args, &x))
  147.         return NULL;
  148.     errno = 0;
  149.     x = frexp(x, &i);
  150.     CHECK(x);
  151.     if (errno != 0)
  152.         return math_error();
  153.     return mkvalue("(di)", x, i);
  154. }
  155.  
  156. static object *
  157. math_ldexp(self, args)
  158.     object *self;
  159.     object *args;
  160. {
  161.     double x, y;
  162.     /* Cheat -- allow float as second argument */
  163.     if (!get2doublearg(args, &x, &y))
  164.         return NULL;
  165.     errno = 0;
  166.     x = ldexp(x, (int)y);
  167.     CHECK(x);
  168.     if (errno != 0)
  169.         return math_error();
  170.     else
  171.         return newfloatobject(x);
  172. }
  173.  
  174. static object *
  175. math_modf(self, args)
  176.     object *self;
  177.     object *args;
  178. {
  179.     double x, y;
  180.     if (!getdoublearg(args, &x))
  181.         return NULL;
  182.     errno = 0;
  183.     x = modf(x, &y);
  184.     CHECK(x);
  185.     if (errno != 0)
  186.         return math_error();
  187.     return mkvalue("(dd)", x, y);
  188. }
  189.  
  190. static struct methodlist math_methods[] = {
  191.     {"acos", math_acos},
  192.     {"asin", math_asin},
  193.     {"atan", math_atan},
  194.     {"atan2", math_atan2},
  195.     {"ceil", math_ceil},
  196.     {"cos", math_cos},
  197.     {"cosh", math_cosh},
  198.     {"exp", math_exp},
  199.     {"fabs", math_fabs},
  200.     {"floor", math_floor},
  201.     {"fmod", math_fmod},
  202.     {"frexp", math_frexp},
  203.     {"ldexp", math_ldexp},
  204.     {"log", math_log},
  205.     {"log10", math_log10},
  206.     {"modf", math_modf},
  207.     {"pow", math_pow},
  208.     {"sin", math_sin},
  209.     {"sinh", math_sinh},
  210.     {"sqrt", math_sqrt},
  211.     {"tan", math_tan},
  212.     {"tanh", math_tanh},
  213.     {NULL,        NULL}        /* sentinel */
  214. };
  215.  
  216. void
  217. initmath()
  218. {
  219.     object *m, *d, *v;
  220.     
  221.     m = initmodule("math", math_methods);
  222.     d = getmoduledict(m);
  223.     dictinsert(d, "pi", v = newfloatobject(atan(1.0) * 4.0));
  224.     DECREF(v);
  225.     dictinsert(d, "e", v = newfloatobject(exp(1.0)));
  226.     DECREF(v);
  227. }
  228.