Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id B07F69FA31B for ; Wed, 27 Dec 2006 18:48:43 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-new, port 10024) with ESMTP id 99330-07 for ; Wed, 27 Dec 2006 18:48:32 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from out4.smtp.messagingengine.com (out4.smtp.messagingengine.com [66.111.4.28]) by postgresql.org (Postfix) with ESMTP id 854989FA0B7 for ; Wed, 27 Dec 2006 18:48:36 -0400 (AST) Received: from out1.internal (unknown [10.202.2.149]) by out1.messagingengine.com (Postfix) with ESMTP id 28A6F5A06E; Wed, 27 Dec 2006 17:47:30 -0500 (EST) Received: from heartbeat2.messagingengine.com ([10.202.2.161]) by out1.internal (MEProxy); Wed, 27 Dec 2006 17:47:30 -0500 X-Sasl-enc: 4SiD6jaD9ylFTxKBXBjM+m7M68JqsJFlQ5SyHuVsmxoS 1167259649 Received: from [10.10.0.130] (adsl-68-75-163-199.dsl.emhril.ameritech.net [68.75.163.199]) by mail.messagingengine.com (Postfix) with ESMTP id 7FCBFFA14; Wed, 27 Dec 2006 17:47:29 -0500 (EST) Message-ID: <4592F842.2000200@yahoo.com> Date: Wed, 27 Dec 2006 16:48:34 -0600 From: Roman Kononov User-Agent: Thunderbird 1.5.0.8 (X11/20061117) MIME-Version: 1.0 To: Bruce Momjian CC: PostgreSQL-patches Subject: Re: [BUGS] BUG #2846: inconsistent and confusing handling of underflows, References: <200612272204.kBRM4Yw13461@momjian.us> In-Reply-To: <200612272204.kBRM4Yw13461@momjian.us> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=4.12 tagged_above=0 required=5 tests=AWL, BAYES_50, DNS_FROM_RFC_ABUSE, DNS_FROM_RFC_POST, DNS_FROM_RFC_WHOIS, FORGED_YAHOO_RCVD X-Spam-Level: **** X-Archive-Number: 200612/166 X-Sequence-Number: 21842 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 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