This is the mail archive of the newlib@sourceware.org mailing list for the newlib project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] hypotf fails for denormal inputs


The hypotf function in newlib gives incorrect results for denormal input values. The problem occurs when the values are scaled by 2^126 because the scaling value is incorrectly specified without the exponent bias. The test program below will demonstrate the problem -- without the patch, the value returned by hypotf will be zero.

(I noticed that other small but non-denormal values are scaled by 2^68 by adding 68 to the exponent, in which case the exponent bias is not included. For the denorm case, the value must be multiplied so the result will be normalized. Presumably someone overlooked this distinction and forgot to add the exponent bias for the denorm case.)


#include <math.h> unsigned int ta = 0x80000002; main () { float a = *(float *)&ta; float c = hypotf(a, a); float d = hypot(a, a); printf("a = %e hypotf(a,a) = %e hypot(a,a) = %e\n", a, c, d); }


2005-08-01 Bob Wilson <bob.wilson@acm.org>


        * libm/math/ef_hypot.c (__ieee754_hypotf): Add missing exponent bias
        to the value for 2^126.

Index: ef_hypot.c
===================================================================
RCS file: /cvs/src/src/newlib/libm/math/ef_hypot.c,v
retrieving revision 1.3
diff -u -p -r1.3 ef_hypot.c
--- ef_hypot.c	22 Mar 2002 11:25:15 -0000	1.3
+++ ef_hypot.c	1 Aug 2005 18:55:07 -0000
@@ -50,7 +50,7 @@
 	    if(FLT_UWORD_IS_ZERO(hb)) {
 	        return a;
 	    } else if(FLT_UWORD_IS_SUBNORMAL(hb)) {
-		SET_FLOAT_WORD(t1,0x3f000000L);	/* t1=2^126 */
+		SET_FLOAT_WORD(t1,0x7e800000L);	/* t1=2^126 */
 		b *= t1;
 		a *= t1;
 		k -= 126;

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]