public inbox for newlib@sourceware.org
 help / color / mirror / Atom feed
* Possible issue with pow(-1, NaN) in newlib's libm
@ 2020-02-14  8:11 Nicolas Brunie
  2020-02-14  9:16 ` Corinna Vinschen
  0 siblings, 1 reply; 2+ messages in thread
From: Nicolas Brunie @ 2020-02-14  8:11 UTC (permalink / raw)
  To: newlib

[-- Attachment #1: Type: text/plain, Size: 883 bytes --]

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(""); 
}

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: test_pow_m1_nan.c --]
[-- Type: text/x-c++src; name=test_pow_m1_nan.c, Size: 242 bytes --]

#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;
}

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: newlib_pow_m1.patch --]
[-- Type: text/x-patch; name=newlib_pow_m1.patch, Size: 550 bytes --]

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("");	
 	}
 

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Possible issue with pow(-1, NaN) in newlib's libm
  2020-02-14  8:11 Possible issue with pow(-1, NaN) in newlib's libm Nicolas Brunie
@ 2020-02-14  9:16 ` Corinna Vinschen
  0 siblings, 0 replies; 2+ messages in thread
From: Corinna Vinschen @ 2020-02-14  9:16 UTC (permalink / raw)
  To: newlib

[-- Attachment #1: Type: text/plain, Size: 570 bytes --]

Hi Nicolas,

On Feb 14 09:11, Nicolas Brunie wrote:
> 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 

You are right, thanks for catching!  I pushed your patch.

Next time, please send the patch in `git format-patch' format,
that's easier to apply :)


Thanks,
Corinna

-- 
Corinna Vinschen
Cygwin Maintainer
Red Hat

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-02-14  9:16 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-14  8:11 Possible issue with pow(-1, NaN) in newlib's libm Nicolas Brunie
2020-02-14  9:16 ` Corinna Vinschen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).