Possible issue with pow(-1, NaN) in newlib's libm

Nicolas Brunie <[email protected]>
Newsgroups gmane.comp.lib.newlib
Message-ID <[email protected]>
Hello all, 

First time sender, excuse me if I misspoke. 

I think I may have encountered a bug in the implementation of pow: 
pow(-1.0, NaN) returns 1.0 when it should return NaN. 
Because ix is used to check input vs 1.0 rather than hx, -1.0 is mistaken for 1.0 


Test in attachment: 
Patch suggestion in attachement and below: 

Regards, 
N. Brunie 

diff --git a/newlib/libm/math/e_pow.c b/newlib/libm/math/e_pow.c 
index 6d2a501..5fd28e6 100644 
--- a/newlib/libm/math/e_pow.c 
+++ b/newlib/libm/math/e_pow.c 
@@ -122,7 +122,7 @@ ivln2_l = 1.92596299112661746887e-08; /* 0x3E54AE0B, 0xF85DDF44 =1/ln2 tail*/ 
/* x|y==NaN return NaN unless x==1 then return 1 */ 
if(ix > 0x7ff00000 || ((ix==0x7ff00000)&&(lx!=0)) || 
iy > 0x7ff00000 || ((iy==0x7ff00000)&&(ly!=0))) { 
- if(((ix-0x3ff00000)|lx)==0) return one; 
+ if(((hx-0x3ff00000)|lx)==0) return one; 
else return nan(""); 
}
test_pow_m1_nan.c (text/x-c++src, 242 B)
#include <math.h>
#include <stdio.h>

// local definition to allow link with newlib's libm and system libc
int __errno;

int main(void)
{
    double pow_m1_nan = pow(-1.0, NAN);
    printf("pow(-1.0, nan)=%f\n", pow_m1_nan);

    return 0;
}
newlib_pow_m1.patch (text/x-patch, 550 B)
diff --git a/newlib/libm/math/e_pow.c b/newlib/libm/math/e_pow.c
index 6d2a501..5fd28e6 100644
--- a/newlib/libm/math/e_pow.c
+++ b/newlib/libm/math/e_pow.c
@@ -122,7 +122,7 @@ ivln2_l  =  1.92596299112661746887e-08; /* 0x3E54AE0B, 0xF85DDF44 =1/ln2 tail*/
     /* x|y==NaN return NaN unless x==1 then return 1 */
 	if(ix > 0x7ff00000 || ((ix==0x7ff00000)&&(lx!=0)) ||
 	   iy > 0x7ff00000 || ((iy==0x7ff00000)&&(ly!=0))) {
-	    if(((ix-0x3ff00000)|lx)==0) return one;
+	    if(((hx-0x3ff00000)|lx)==0) return one;
 	    else return nan("");	
 	}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.