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