+// Toss a runtime error for any NaN or Inf values produced. Note that
+// this assumes an IEEE 754 format.
+#define VALIDATE(r) (valid(r.num) ? (r) : die(c, __FUNCTION__+2))
+
+static int valid(double d)
+{
+ union { double d; unsigned long long ull; } u;
+ u.d = d;
+ return ((u.ull >> 52) & 0x7ff) != 0x7ff;
+}
+
+static naRef die(naContext c, const char* fn)
+{
+ naRuntimeError(c, "floating point error in math.%s()", fn);
+ return naNil();
+}
+