blob: 1c232c98316262051633f7a19e673558bc382b20 [file] [log] [blame]
Guido van Rossum52fa3a61997-02-14 22:59:58 +00001/*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002 ---------------------------------------------------------------------
3 / Copyright (c) 1996. \
Guido van Rossum52fa3a61997-02-14 22:59:58 +00004 | 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 |
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000035 \ endorsement purposes. /
36 ---------------------------------------------------------------------
Guido van Rossum52fa3a61997-02-14 22:59:58 +000037*/
38
39/*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000040 Floating point exception test module.
Guido van Rossum52fa3a61997-02-14 22:59:58 +000041
42 */
43
44#include "Python.h"
45
46static PyObject *fpe_error;
Martin v. Löwis1a214512008-06-11 05:26:20 +000047
48PyMODINIT_FUNC PyInit_fpetest(void);
Guido van Rossum52fa3a61997-02-14 22:59:58 +000049static PyObject *test(PyObject *self,PyObject *args);
Guido van Rossum05bc4af1997-02-21 01:22:47 +000050static double db0(double);
51static double overflow(double);
52static double nest1(int, double);
53static double nest2(int, double);
Guido van Rossum52fa3a61997-02-14 22:59:58 +000054static double nest3(double);
Guido van Rossum05bc4af1997-02-21 01:22:47 +000055static void printerr(double);
Guido van Rossum52fa3a61997-02-14 22:59:58 +000056
57static PyMethodDef fpetest_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000058 {"test", (PyCFunction) test, METH_VARARGS},
Guido van Rossum52fa3a61997-02-14 22:59:58 +000059 {0,0}
60};
61
62static PyObject *test(PyObject *self,PyObject *args)
63{
Guido van Rossum05bc4af1997-02-21 01:22:47 +000064 double r;
Guido van Rossum52fa3a61997-02-14 22:59:58 +000065
Guido van Rossum05bc4af1997-02-21 01:22:47 +000066 fprintf(stderr,"overflow");
67 r = overflow(1.e160);
68 printerr(r);
Guido van Rossum52fa3a61997-02-14 22:59:58 +000069
Guido van Rossum05bc4af1997-02-21 01:22:47 +000070 fprintf(stderr,"\ndiv by 0");
71 r = db0(0.0);
72 printerr(r);
Guido van Rossum52fa3a61997-02-14 22:59:58 +000073
Guido van Rossum05bc4af1997-02-21 01:22:47 +000074 fprintf(stderr,"\nnested outer");
75 r = nest1(0, 0.0);
76 printerr(r);
Guido van Rossum52fa3a61997-02-14 22:59:58 +000077
Guido van Rossum05bc4af1997-02-21 01:22:47 +000078 fprintf(stderr,"\nnested inner");
79 r = nest1(1, 1.0);
80 printerr(r);
Guido van Rossum52fa3a61997-02-14 22:59:58 +000081
Guido van Rossum05bc4af1997-02-21 01:22:47 +000082 fprintf(stderr,"\ntrailing outer");
83 r = nest1(2, 2.0);
84 printerr(r);
Guido van Rossum52fa3a61997-02-14 22:59:58 +000085
Guido van Rossum05bc4af1997-02-21 01:22:47 +000086 fprintf(stderr,"\nnested prior");
87 r = nest2(0, 0.0);
88 printerr(r);
Guido van Rossum52fa3a61997-02-14 22:59:58 +000089
Guido van Rossum05bc4af1997-02-21 01:22:47 +000090 fprintf(stderr,"\nnested interior");
91 r = nest2(1, 1.0);
92 printerr(r);
Guido van Rossum52fa3a61997-02-14 22:59:58 +000093
Guido van Rossum05bc4af1997-02-21 01:22:47 +000094 fprintf(stderr,"\nnested trailing");
95 r = nest2(2, 2.0);
96 printerr(r);
Guido van Rossum52fa3a61997-02-14 22:59:58 +000097
Serhiy Storchaka228b12e2017-01-23 09:47:21 +020098 Py_RETURN_NONE;
Guido van Rossum52fa3a61997-02-14 22:59:58 +000099}
100
Guido van Rossum05bc4af1997-02-21 01:22:47 +0000101static void printerr(double r)
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000102{
Guido van Rossum05bc4af1997-02-21 01:22:47 +0000103 if(r == 3.1416){
104 fprintf(stderr,"\tPASS\n");
105 PyErr_Print();
106 }else{
107 fprintf(stderr,"\tFAIL\n");
108 }
109 PyErr_Clear();
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000110}
111
Guido van Rossum05bc4af1997-02-21 01:22:47 +0000112static double nest1(int i, double x)
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000113{
114 double a = 1.0;
Guido van Rossum05bc4af1997-02-21 01:22:47 +0000115
116 PyFPE_START_PROTECT("Division by zero, outer zone", return 3.1416)
117 if(i == 0){
118 a = 1./x;
119 }else if(i == 1){
120 /* This (following) message is never seen. */
121 PyFPE_START_PROTECT("Division by zero, inner zone", return 3.1416)
122 a = 1./(1. - x);
Guido van Rossum45b83911997-03-14 04:32:50 +0000123 PyFPE_END_PROTECT(a)
Guido van Rossum05bc4af1997-02-21 01:22:47 +0000124 }else if(i == 2){
125 a = 1./(2. - x);
126 }
Guido van Rossum45b83911997-03-14 04:32:50 +0000127 PyFPE_END_PROTECT(a)
Guido van Rossum05bc4af1997-02-21 01:22:47 +0000128
129 return a;
130}
131
132static double nest2(int i, double x)
133{
134 double a = 1.0;
135 PyFPE_START_PROTECT("Division by zero, prior error", return 3.1416)
136 if(i == 0){
137 a = 1./x;
138 }else if(i == 1){
139 a = nest3(x);
140 }else if(i == 2){
141 a = 1./(2. - x);
142 }
Guido van Rossum45b83911997-03-14 04:32:50 +0000143 PyFPE_END_PROTECT(a)
Guido van Rossum05bc4af1997-02-21 01:22:47 +0000144 return a;
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000145}
146
147static double nest3(double x)
148{
149 double result;
Guido van Rossum05bc4af1997-02-21 01:22:47 +0000150 /* This (following) message is never seen. */
151 PyFPE_START_PROTECT("Division by zero, nest3 error", return 3.1416)
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000152 result = 1./(1. - x);
Guido van Rossum45b83911997-03-14 04:32:50 +0000153 PyFPE_END_PROTECT(result)
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000154 return result;
155}
156
Guido van Rossum05bc4af1997-02-21 01:22:47 +0000157static double db0(double x)
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000158{
Guido van Rossum05bc4af1997-02-21 01:22:47 +0000159 double a;
160 PyFPE_START_PROTECT("Division by zero", return 3.1416)
161 a = 1./x;
Guido van Rossum45b83911997-03-14 04:32:50 +0000162 PyFPE_END_PROTECT(a)
Guido van Rossum05bc4af1997-02-21 01:22:47 +0000163 return a;
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000164}
165
Guido van Rossum05bc4af1997-02-21 01:22:47 +0000166static double overflow(double b)
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000167{
Guido van Rossum05bc4af1997-02-21 01:22:47 +0000168 double a;
169 PyFPE_START_PROTECT("Overflow", return 3.1416)
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000170 a = b*b;
Guido van Rossum45b83911997-03-14 04:32:50 +0000171 PyFPE_END_PROTECT(a)
Guido van Rossum05bc4af1997-02-21 01:22:47 +0000172 return a;
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000173}
174
Martin v. Löwis1a214512008-06-11 05:26:20 +0000175static struct PyModuleDef fpetestmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000176 PyModuleDef_HEAD_INIT,
177 "fpetest",
178 NULL,
179 -1,
180 fpetest_methods,
181 NULL,
182 NULL,
183 NULL,
184 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000185};
186
187PyMODINIT_FUNC PyInit_fpetest(void)
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000188{
189 PyObject *m, *d;
190
Martin v. Löwis1a214512008-06-11 05:26:20 +0000191 m = PyModule_Create(&fpetestmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000192 if (m == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000193 return NULL;
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000194 d = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000195 fpe_error = PyErr_NewException("fpetest.error", NULL, NULL);
196 if (fpe_error != NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 PyDict_SetItemString(d, "error", fpe_error);
Martin v. Löwis1a214512008-06-11 05:26:20 +0000198 return m;
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000199}