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