Skip site navigation (1) Skip section navigation (2)

Re: [BUGS] BUG #2846: inconsistent and confusing handling of underflows,

From: Roman Kononov <kononov195-pgsql(at)yahoo(dot)com>
To: Bruce Momjian <bruce(at)momjian(dot)us>
Cc: PostgreSQL-patches <pgsql-patches(at)postgresql(dot)org>
Subject: Re: [BUGS] BUG #2846: inconsistent and confusing handling of underflows,
Date: 2006-12-27 22:48:34
Message-ID: 4592F842.2000200@yahoo.com (view raw or flat)
Thread:
Lists: pgsql-hackerspgsql-patches
On 12/27/2006 04:04 PM, Bruce Momjian wrote:
> Interesting.  I didn't know that, but in the float4pl() function,
> because the overflow tests and result is float4, what value is there to
> doing things as double --- as soon as the float4 maximum is exceeded, we
> throw an error?
> 

This is useful for underflows.

	float a=1e-30;
	float b=1e-30;
	double r1=a*b;
	double r2=(double)a*b;

r1 is zero and underflow is lost.
r2 is not zero and underflow is detected.

In float4mul() and float4div(), the computation should be double precision.

In float4pl() and float4mi(), it depends on the ability of the hardware
to generate denormalized numbers. If denormalized numbers are generated,
float vs double makes no difference. If denormalized numbers are not
generated (zero is generated), then double computation is safer.

Another way to detect underflows, overflows and other junk is to use FPU
status flags after each computation. Performance will likely suffer.

#include <fenv.h>

Datum
float4mul(PG_FUNCTION_ARGS)
{
     float4   arg1 = PG_GETARG_FLOAT4(0);
     float4   arg2 = PG_GETARG_FLOAT4(1);
     float4   result;
     int fe_exceptions;
     feclearexcept(FE_ALL_EXCEPT);
     result = arg1 * arg2;
     fe_exceptions=fetestexcept(FE_DIVBYZERO,FE_INVALID,FE_OVERFLOW,FE_UNDERFLOW);
     if (fe_exceptions) handle_exceptions(fe_exceptions); //??
     PG_RETURN_FLOAT4(result);
}

Yet another way to detect exceptions is to remove all CheckFloat4Val(),
CheckFloat8Val(), isnan(), unmask FPU exceptions and install an exception handler.
Might have portability difficulties. Comparisons of NaNs must be done in
non-IEEE way (FPU does not compare NaNs, it generates exceptions).

Roman


In response to

Responses

pgsql-hackers by date

Next:From: Andrew DunstanDate: 2006-12-27 22:53:28
Subject: Re: pg_hba.conf hostname todo
Previous:From: Tom LaneDate: 2006-12-27 22:44:16
Subject: Re: [BUGS] BUG #2846: inconsistent and confusing handling of

pgsql-patches by date

Next:From: Simon RiggsDate: 2006-12-27 22:54:57
Subject: Re: Load distributed checkpoint
Previous:From: Tom LaneDate: 2006-12-27 22:44:16
Subject: Re: [BUGS] BUG #2846: inconsistent and confusing handling of

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group