You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

200 lines
5.7 KiB

  1. /*
  2. ---------------------------------------------------------------------
  3. / Copyright (c) 1996. \
  4. | The Regents of the University of California. |
  5. | All rights reserved. |
  6. | |
  7. | Permission to use, copy, modify, and distribute this software for |
  8. | any purpose without fee is hereby granted, provided that this en- |
  9. | tire notice is included in all copies of any software which is or |
  10. | includes a copy or modification of this software and in all |
  11. | copies of the supporting documentation for such software. |
  12. | |
  13. | This work was produced at the University of California, Lawrence |
  14. | Livermore National Laboratory under contract no. W-7405-ENG-48 |
  15. | between the U.S. Department of Energy and The Regents of the |
  16. | University of California for the operation of UC LLNL. |
  17. | |
  18. | DISCLAIMER |
  19. | |
  20. | This software was prepared as an account of work sponsored by an |
  21. | agency of the United States Government. Neither the United States |
  22. | Government nor the University of California nor any of their em- |
  23. | ployees, makes any warranty, express or implied, or assumes any |
  24. | liability or responsibility for the accuracy, completeness, or |
  25. | usefulness of any information, apparatus, product, or process |
  26. | disclosed, or represents that its use would not infringe |
  27. | privately-owned rights. Reference herein to any specific commer- |
  28. | cial products, process, or service by trade name, trademark, |
  29. | manufacturer, or otherwise, does not necessarily constitute or |
  30. | imply its endorsement, recommendation, or favoring by the United |
  31. | States Government or the University of California. The views and |
  32. | opinions of authors expressed herein do not necessarily state or |
  33. | reflect those of the United States Government or the University |
  34. | of California, and shall not be used for advertising or product |
  35. \ endorsement purposes. /
  36. ---------------------------------------------------------------------
  37. */
  38. /*
  39. Floating point exception test module.
  40. */
  41. #include "Python.h"
  42. static PyObject *fpe_error;
  43. PyMODINIT_FUNC PyInit_fpetest(void);
  44. static PyObject *test(PyObject *self,PyObject *args);
  45. static double db0(double);
  46. static double overflow(double);
  47. static double nest1(int, double);
  48. static double nest2(int, double);
  49. static double nest3(double);
  50. static void printerr(double);
  51. static PyMethodDef fpetest_methods[] = {
  52. {"test", (PyCFunction) test, METH_VARARGS},
  53. {0,0}
  54. };
  55. static PyObject *test(PyObject *self,PyObject *args)
  56. {
  57. double r;
  58. fprintf(stderr,"overflow");
  59. r = overflow(1.e160);
  60. printerr(r);
  61. fprintf(stderr,"\ndiv by 0");
  62. r = db0(0.0);
  63. printerr(r);
  64. fprintf(stderr,"\nnested outer");
  65. r = nest1(0, 0.0);
  66. printerr(r);
  67. fprintf(stderr,"\nnested inner");
  68. r = nest1(1, 1.0);
  69. printerr(r);
  70. fprintf(stderr,"\ntrailing outer");
  71. r = nest1(2, 2.0);
  72. printerr(r);
  73. fprintf(stderr,"\nnested prior");
  74. r = nest2(0, 0.0);
  75. printerr(r);
  76. fprintf(stderr,"\nnested interior");
  77. r = nest2(1, 1.0);
  78. printerr(r);
  79. fprintf(stderr,"\nnested trailing");
  80. r = nest2(2, 2.0);
  81. printerr(r);
  82. Py_INCREF (Py_None);
  83. return Py_None;
  84. }
  85. static void printerr(double r)
  86. {
  87. if(r == 3.1416){
  88. fprintf(stderr,"\tPASS\n");
  89. PyErr_Print();
  90. }else{
  91. fprintf(stderr,"\tFAIL\n");
  92. }
  93. PyErr_Clear();
  94. }
  95. static double nest1(int i, double x)
  96. {
  97. double a = 1.0;
  98. PyFPE_START_PROTECT("Division by zero, outer zone", return 3.1416)
  99. if(i == 0){
  100. a = 1./x;
  101. }else if(i == 1){
  102. /* This (following) message is never seen. */
  103. PyFPE_START_PROTECT("Division by zero, inner zone", return 3.1416)
  104. a = 1./(1. - x);
  105. PyFPE_END_PROTECT(a)
  106. }else if(i == 2){
  107. a = 1./(2. - x);
  108. }
  109. PyFPE_END_PROTECT(a)
  110. return a;
  111. }
  112. static double nest2(int i, double x)
  113. {
  114. double a = 1.0;
  115. PyFPE_START_PROTECT("Division by zero, prior error", return 3.1416)
  116. if(i == 0){
  117. a = 1./x;
  118. }else if(i == 1){
  119. a = nest3(x);
  120. }else if(i == 2){
  121. a = 1./(2. - x);
  122. }
  123. PyFPE_END_PROTECT(a)
  124. return a;
  125. }
  126. static double nest3(double x)
  127. {
  128. double result;
  129. /* This (following) message is never seen. */
  130. PyFPE_START_PROTECT("Division by zero, nest3 error", return 3.1416)
  131. result = 1./(1. - x);
  132. PyFPE_END_PROTECT(result)
  133. return result;
  134. }
  135. static double db0(double x)
  136. {
  137. double a;
  138. PyFPE_START_PROTECT("Division by zero", return 3.1416)
  139. a = 1./x;
  140. PyFPE_END_PROTECT(a)
  141. return a;
  142. }
  143. static double overflow(double b)
  144. {
  145. double a;
  146. PyFPE_START_PROTECT("Overflow", return 3.1416)
  147. a = b*b;
  148. PyFPE_END_PROTECT(a)
  149. return a;
  150. }
  151. static struct PyModuleDef fpetestmodule = {
  152. PyModuleDef_HEAD_INIT,
  153. "fpetest",
  154. NULL,
  155. -1,
  156. fpetest_methods,
  157. NULL,
  158. NULL,
  159. NULL,
  160. NULL
  161. };
  162. PyMODINIT_FUNC PyInit_fpetest(void)
  163. {
  164. PyObject *m, *d;
  165. m = PyModule_Create(&fpetestmodule);
  166. if (m == NULL)
  167. return NULL;
  168. d = PyModule_GetDict(m);
  169. fpe_error = PyErr_NewException("fpetest.error", NULL, NULL);
  170. if (fpe_error != NULL)
  171. PyDict_SetItemString(d, "error", fpe_error);
  172. return m;
  173. }