The following code return a -nan PDL for both values. Expected the first to be nan and the second to be -nan.
#!/usr/bin/perl
use strict;
use warnings;
use PDL;
my $c = PDL->new("nan");
print "nan: ".ref($c)." ".$c."\n";
$c = PDL->new("-nan");
print "-nan: ".ref($c)." ".$c."\n";
Matthew McGillis
2012-06-16
I have done some digging on this and a piece of this is probably in libc however something is also going on in pdl. What I would hope is that assuming a consistent libc for each of the following pieces of code:
1) infnan.c
2) perl.pl
3) pdl.pl
then each should produce identical computed results. However for me even with a consistent libc I do not get consistent results between the three.
My results:
1) infnan.c
+ -nan -inf -1 -0 0 1 inf nan
-nan -nan -nan -nan -nan -nan -nan -nan nan
-inf -nan -inf -inf -inf -inf -inf -nan nan
-1 -nan -inf -2 -1 -1 0 inf nan
-0 -nan -inf -1 -0 0 1 inf nan
0 -nan -inf -1 0 0 1 inf nan
1 -nan -inf 0 1 1 2 inf nan
inf -nan -nan inf inf inf inf inf nan
nan -nan nan nan nan nan nan nan nan
- -nan -inf -1 -0 0 1 inf nan
-nan -nan -nan -nan -nan -nan -nan -nan -nan
-inf -nan -nan -inf -inf -inf -inf -inf nan
-1 -nan inf 0 -1 -1 -2 -inf nan
-0 -nan inf 1 0 -0 -1 -inf nan
0 -nan inf 1 0 0 -1 -inf nan
1 -nan inf 2 1 1 0 -inf nan
inf -nan inf inf inf inf inf -nan nan
nan nan nan nan nan nan nan nan nan
* -nan -inf -1 -0 0 1 inf nan
-nan -nan -nan -nan -nan -nan -nan -nan nan
-inf -nan inf inf -nan -nan -inf -inf nan
-1 -nan inf 1 0 -0 -1 -inf nan
-0 -nan -nan 0 0 -0 -0 -nan nan
0 -nan -nan -0 -0 0 0 -nan nan
1 -nan -inf -1 -0 0 1 inf nan
inf -nan -inf -inf -nan -nan inf inf nan
nan -nan nan nan nan nan nan nan nan
/ -nan -inf -1 -0 0 1 inf nan
-nan -nan -nan -nan -nan -nan -nan -nan -nan
-inf -nan -nan inf inf -inf -inf -nan nan
-1 -nan 0 1 inf -inf -1 -0 nan
-0 -nan 0 0 -nan -nan -0 -0 nan
0 -nan -0 -0 -nan -nan 0 0 nan
1 -nan -0 -1 -inf inf 1 0 nan
inf -nan -nan -inf -inf inf inf -nan nan
nan nan nan nan nan nan nan nan nan
2) perl.pl
+ nan -inf -1 0 0 1 inf nan
nan nan nan nan nan nan nan nan nan
-inf nan nan inf -inf -inf -inf nan nan
-1 nan 0 1 -inf -inf -1 0 nan
0 nan 0 0 nan nan 0 0 nan
0 nan 0 0 nan nan 0 0 nan
1 nan 0 -1 inf inf 1 0 nan
inf nan nan -inf inf inf inf nan nan
nan nan nan nan nan nan nan nan nan
- nan -inf -1 0 0 1 inf nan
nan nan nan nan nan nan nan nan nan
-inf nan nan inf -inf -inf -inf nan nan
-1 nan 0 1 -inf -inf -1 0 nan
0 nan 0 0 nan nan 0 0 nan
0 nan 0 0 nan nan 0 0 nan
1 nan 0 -1 inf inf 1 0 nan
inf nan nan -inf inf inf inf nan nan
nan nan nan nan nan nan nan nan nan
* nan -inf -1 0 0 1 inf nan
nan nan nan nan nan nan nan nan nan
-inf nan nan inf -inf -inf -inf nan nan
-1 nan 0 1 -inf -inf -1 0 nan
0 nan 0 0 nan nan 0 0 nan
0 nan 0 0 nan nan 0 0 nan
1 nan 0 -1 inf inf 1 0 nan
inf nan nan -inf inf inf inf nan nan
nan nan nan nan nan nan nan nan nan
/ nan -inf -1 0 0 1 inf nan
nan nan nan nan nan nan nan nan nan
-inf nan nan inf -inf -inf -inf nan nan
-1 nan 0 1 -inf -inf -1 0 nan
0 nan 0 0 nan nan 0 0 nan
0 nan 0 0 nan nan 0 0 nan
1 nan 0 -1 inf inf 1 0 nan
inf nan nan -inf inf inf inf nan nan
nan nan nan nan nan nan nan nan nan
3) pdl.pl
[-nan -inf -1 0 0 1 inf nan]
[
[-nan]
[-inf]
[ -1]
[ 0]
[ 0]
[ 1]
[ inf]
[ nan]
]
[
[-nan -nan -nan -nan -nan -nan -nan nan]
[-nan -inf -inf -inf -inf -inf -nan nan]
[-nan -inf -2 -1 -1 0 inf nan]
[-nan -inf -1 0 0 1 inf nan]
[-nan -inf -1 0 0 1 inf nan]
[-nan -inf 0 1 1 2 inf nan]
[-nan -nan inf inf inf inf inf nan]
[-nan nan nan nan nan nan nan nan]
]
[
[-nan -nan -nan -nan -nan -nan -nan nan]
[-nan -nan inf inf inf inf inf nan]
[-nan -inf 0 1 1 2 inf nan]
[-nan -inf -1 0 0 1 inf nan]
[-nan -inf -1 0 0 1 inf nan]
[-nan -inf -2 -1 -1 0 inf nan]
[-nan -inf -inf -inf -inf -inf -nan nan]
[-nan nan nan nan nan nan nan nan]
]
[
[-nan -nan -nan -nan -nan -nan -nan nan]
[-nan inf inf -nan -nan -inf -inf nan]
[-nan inf 1 -0 -0 -1 -inf nan]
[-nan -nan -0 0 0 0 -nan nan]
[-nan -nan -0 0 0 0 -nan nan]
[-nan -inf -1 0 0 1 inf nan]
[-nan -inf -inf -nan -nan inf inf nan]
[-nan nan nan nan nan nan nan nan]
]
[
[-nan -nan -nan -nan -nan -nan -nan nan]
[-nan -nan 0 -0 -0 -0 -nan nan]
[-nan inf 1 -0 -0 -1 -inf nan]
[-nan -inf -inf -nan -nan inf inf nan]
[-nan -inf -inf -nan -nan inf inf nan]
[-nan -inf -1 0 0 1 inf nan]
[-nan -nan -0 0 0 0 -nan nan]
[-nan nan nan nan nan nan nan nan]
]
Matthew McGillis
2012-06-16
C Code
Matthew McGillis
2012-06-16
Perl Code
Matthew McGillis
2012-06-16
PDL Code
Matthew McGillis
2012-07-03
I really think the 2.4.10 and above releases of PDL have introduced some bugs that need to be resolved related to this. But, since so far this has not prompted anyone to resolve and it appears now to be flagged as a feature rather than bug I have attached the following:
1) A regression test script "scalar.t" that can be used on future versions of PDL to validate that PDL maintains consistent behavior for scaler values. I think the set of tests I have in it are reasonable although it could be easily expanded to be more comprehensive.
2) A diff for the file Basic/Core/Core.pm.PL of the changes I made to 2.4.11 to get the above script to pass all tests.
With the above any chance I can get an idea if/when the above might get included into a future release of PDL?
Matthew McGillis
2012-07-03
File diff of changes to resolve this bug.
Matthew McGillis
2012-07-03
Regression tests to validate PDL scalar values remain consistent.
David Mertens
2012-07-03
David Mertens
2012-07-03
David Mertens
2012-07-03
This particular issue has been fixed in git with commit 000212d. I did not include the test suite that you provided as most of it was redundant with other tests. I found that I was able to ascertain the sign of the nan by stringifying the value and checking for a dash in the string, and was thereby able to side-step the need to use Inline::Pdlpp to check the sign.
With this patch, I consider this bug resolved. However, there is the related bug that Perl and PDL do not differentiate between -0 and +0. This was included in the original test suite that you provided, but was not added to the current test suite because I am not sure how to manufacture a negative zero from within Perl. I will be happy to include the test once we have a means for manufacturing such a value.
Thanks for your persistence so far!