On 12/29/2006 12:23 AM, Bruce Momjian wrote:
Well, then show me what direction you think is better.

Think about this idea please. This has no INF, NaN or range
checks and detects all "bad" cases with any floating point
math.

The only issue is that a bad case is detected only once.
You need to restart the postmaster. It can be fixed by
re-enabling FP exceptions in the FP exception handler.

Roman
-----------------------------
~/postgresql-8.2.0/src/backend/utils/adt>diff -U3 -p float.orig.c float.c
--- float.orig.c        2006-12-29 10:49:51.000000000 -0600
+++ float.c     2006-12-29 10:58:19.000000000 -0600
@@ -60,12 +60,21 @@
 #ifdef HAVE_IEEEFP_H
 #include <ieeefp.h>
 #endif
+#include <fenv.h>

 #include "catalog/pg_type.h"
 #include "libpq/pqformat.h"
 #include "utils/array.h"
 #include "utils/builtins.h"

+static void __attribute__((__constructor__))
+enable_fp_exceptions()
+{
+       feclearexcept(FE_ALL_EXCEPT);
+       feenableexcept(FE_DIVBYZERO | FE_UNDERFLOW | FE_OVERFLOW | FE_INVALID);
+       printf("FP exceptions enabled\n");
+}
+

 #ifndef M_PI
 /* from my RH5.2 gcc math.h file - thomas 2000-04-03 */
@@ -783,11 +792,10 @@ float4pl(PG_FUNCTION_ARGS)
 {
        float4          arg1 = PG_GETARG_FLOAT4(0);
        float4          arg2 = PG_GETARG_FLOAT4(1);
-       double          result;
+       float4          result;

        result = arg1 + arg2;
-       CheckFloat4Val(result);
-       PG_RETURN_FLOAT4((float4) result);
+       PG_RETURN_FLOAT4(result);
 }

 Datum
@@ -795,11 +803,10 @@ float4mi(PG_FUNCTION_ARGS)
 {
        float4          arg1 = PG_GETARG_FLOAT4(0);
        float4          arg2 = PG_GETARG_FLOAT4(1);
-       double          result;
+       float4          result;

        result = arg1 - arg2;
-       CheckFloat4Val(result);
-       PG_RETURN_FLOAT4((float4) result);
+       PG_RETURN_FLOAT4(result);
 }

 Datum
@@ -807,11 +814,10 @@ float4mul(PG_FUNCTION_ARGS)
 {
        float4          arg1 = PG_GETARG_FLOAT4(0);
        float4          arg2 = PG_GETARG_FLOAT4(1);
-       double          result;
+       float4          result;

        result = arg1 * arg2;
-       CheckFloat4Val(result);
-       PG_RETURN_FLOAT4((float4) result);
+       PG_RETURN_FLOAT4(result);
 }

 Datum
@@ -819,18 +825,10 @@ float4div(PG_FUNCTION_ARGS)
 {
        float4          arg1 = PG_GETARG_FLOAT4(0);
        float4          arg2 = PG_GETARG_FLOAT4(1);
-       double          result;
-
-       if (arg2 == 0.0)
-               ereport(ERROR,
-                               (errcode(ERRCODE_DIVISION_BY_ZERO),
-                                errmsg("division by zero")));
-
-       /* Do division in float8, then check for overflow */
-       result = (float8) arg1 / (float8) arg2;
+       float4          result;

-       CheckFloat4Val(result);
-       PG_RETURN_FLOAT4((float4) result);
+       result = arg1 / arg2;
+       PG_RETURN_FLOAT4(result);
 }

 /*


---------------------------(end of broadcast)---------------------------
TIP 5: don't forget to increase your free space map settings

Reply via email to