$NetBSD: patch-ad,v 1.1 2002/03/22 15:20:24 drochner Exp $

--- Include/pyport.h.orig	Sat Oct 27 23:16:16 2001
+++ Include/pyport.h	Fri Mar 22 15:17:29 2002
@@ -285,6 +285,41 @@
 			errno = ERANGE; \
 	} while(0)
 
+/* Py_ADJUST_ERANGE1(x)
+ * Py_ADJUST_ERANGE2(x, y)
+ * Set errno to 0 before calling a libm function, and invoke one of these
+ * macros after, passing the function result(s) (Py_ADJUST_ERANGE2 is useful
+ * for functions returning complex results).  This makes two kinds of
+ * adjustments to errno:  (A) If it looks like the platform libm set
+ * errno=ERANGE due to underflow, clear errno. (B) If it looks like the
+ * platform libm overflowed but didn't set errno, force errno to ERANGE.  In
+ * effect, we're trying to force a useful implementation of C89 errno
+ * behavior.
+ * Caution:
+ *    This isn't reliable.  See Py_OVERFLOWED comments.
+ *    X and Y may be evaluated more than once.
+ */
+#define Py_ADJUST_ERANGE1(X)                                            \
+        do {                                                            \
+                if (errno == 0) {                                       \
+                        if ((X) == Py_HUGE_VAL || (X) == -Py_HUGE_VAL)  \
+                                errno = ERANGE;                         \
+                }                                                       \
+                else if (errno == ERANGE && (X) == 0.0)                 \
+                        errno = 0;                                      \
+        } while(0)
+
+#define Py_ADJUST_ERANGE2(X, Y)                                         \
+        do {                                                            \
+                if ((X) == Py_HUGE_VAL || (X) == -Py_HUGE_VAL ||        \
+                    (Y) == Py_HUGE_VAL || (Y) == -Py_HUGE_VAL) {        \
+                                if (errno == 0)                         \
+                                        errno = ERANGE;                 \
+                }                                                       \
+                else if (errno == ERANGE)                               \
+                        errno = 0;                                      \
+        } while(0)
+
 /**************************************************************************
 Prototypes that are missing from the standard include files on some systems
 (and possibly only some versions of such systems.)
