Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   IEEE NaN screwed up? (http://www.velocityreviews.com/forums/t893405-ieee-nan-screwed-up.html)

ivowel@gmail.com 07-23-2005 02:59 AM

IEEE NaN screwed up?
 

dear perl wizards:

perl v5.8.5, gentoo linux:

my $v="NA"; my $x= $v+1.0; print $x; --> some error about adding
strings
my $v="NaN"; my $x= $v+1.0; print $x; --> 1

this seems wrong. adding a NaN and a number should be a NaN; $v should
not be interpreted as 0. am I missing something obvious?

sincerely,

/iaw


attn.steven.kuo@gmail.com 07-23-2005 03:38 AM

Re: IEEE NaN screwed up?
 
ivowel@gmail.com wrote:
> dear perl wizards:
>
> perl v5.8.5, gentoo linux:
>
> my $v="NA"; my $x= $v+1.0; print $x; --> some error about adding
> strings
> my $v="NaN"; my $x= $v+1.0; print $x; --> 1
>
> this seems wrong. adding a NaN and a number should be a NaN; $v should
> not be interpreted as 0. am I missing something obvious?
>
> sincerely,
>
> /iaw



See:

$ perldoc bignum

$ perldoc bigint

$ perl -Mbignum -le 'print NaN + 1.0'
NaN


Note that NaN is not a quoted string.

--
Hope this helps,
Steven


ivowel@gmail.com 07-23-2005 12:27 PM

Re: IEEE NaN screwed up?
 

hi steven:

yes it does help, because I now know that I need BigFloat, but the perl
default treatment still seems like a big. if NaN is the IEEE quantity
(or something similar), then $x should be NaN. if NaN is just a
string, then perl should give an error at the addition, just as it
would if the string were NA.

regards,

/iaw


attn.steven.kuo@gmail.com 07-23-2005 04:42 PM

Re: IEEE NaN screwed up?
 
ivowel@gmail.com wrote:
> hi steven:
>
> yes it does help, because I now know that I need BigFloat, but the perl
> default treatment still seems like a big. if NaN is the IEEE quantity
> (or something similar), then $x should be NaN. if NaN is just a
> string, then perl should give an error at the addition, just as it
> would if the string were NA.
>
> regards,
>
> /iaw



Yes, if you're unfamiliar with
Perl internals, then the above
does seem confusing.

A scalar in Perl can be a string,
a number, or a string and a
number at the same time.


One can use the Devel::Peek module
to look at the underlying structure
of a scalar (as it's "upgraded"
as the program advances).

Here's an example:


use strict;
use warnings;
use Devel::Peek;


my $v = "NA";

Dump $v;

my $x = $v + 1.0; # warning expected

Dump $v;

$v = "NAN";

Dump $v;

$x = $v + 1.0; # no warning here!

Dump $v;



== The results (and my comments) ==

SV = PV(0xf5870) at 0xffb98
REFCNT = 1
FLAGS = (PADBUSY,PADMY,POK,pPOK)
PV = 0xfbef0 "NA"\0
CUR = 2
LEN = 3

:: At this point $v is a string,
the scalar is a PV, and PV has
the value "NA". Thus we
see the following warning upon
attempted addition:


Argument "NA" isn't numeric in addition (+) at explain.pl line 11.


SV = PVNV(0x137a50) at 0xffb98
REFCNT = 1
FLAGS = (PADBUSY,PADMY,IOK,NOK,POK,pIOK,pNOK,pPOK)
IV = 0
NV = 0
PV = 0xfbef0 "NA"\0
CUR = 2
LEN = 3

:: The attempt at addition, however,
causes the scalar to be upgraded to
a PVNV. Now the scalar has not
only the string representation,
"NA", but also numeric representations
in the NV and IV slots.



SV = PVNV(0x137a50) at 0xffb98
REFCNT = 1
FLAGS = (PADBUSY,PADMY,POK,pPOK)
IV = 0
NV = 0
PV = 0xfbef0 "NAN"\0
CUR = 3
LEN = 4

:: We reassign to the PV slot of
the scalar, replacing "NA" with "NAN".
The second attempt at addition, however,
produces no warnings.


SV = PVNV(0x137a50) at 0xffb98
REFCNT = 1
FLAGS = (PADBUSY,PADMY,IOK,NOK,POK,pIOK,pNOK,pPOK)
IV = 0
NV = 0
PV = 0xfbef0 "NAN"\0
CUR = 3
LEN = 4


:: That because, the addition operation
is using the value in the NV slot
of the scalar -- which was initialized
to zero. 0 + 1.0 is 1.0; no warnings
because perl was trying to DWYM
(Do What You Meant).

--
Hope this helps,
Steven


attn.steven.kuo@gmail.com 07-23-2005 05:00 PM

Re: IEEE NaN screwed up?
 
attn.steven.kuo@gmail.com wrote:

(snipped)

> One can use the Devel::Peek module
> to look at the underlying structure
> of a scalar (as it's "upgraded"
> as the program advances).

....


Bah! If I replace "NAN"
with "foo" then I get a
second warning about

Argument "foo" isn't numeric in addition (+) at explain.pl line 20

Ignore my last posting... there is
something different about "NAN". Sorry
about the misinformation.

--
Regards,
Steven


Sisyphus 07-24-2005 12:19 AM

Re: IEEE NaN screwed up?
 

<attn.steven.kuo@gmail.com> wrote in message
news:1122138001.481423.193540@g43g2000cwa.googlegr oups.com...
> attn.steven.kuo@gmail.com wrote:
>
> (snipped)
>
> > One can use the Devel::Peek module
> > to look at the underlying structure
> > of a scalar (as it's "upgraded"
> > as the program advances).

> ...
>
>
> Bah! If I replace "NAN"
> with "foo" then I get a
> second warning about
>
> Argument "foo" isn't numeric in addition (+) at explain.pl line 20
>
> Ignore my last posting... there is
> something different about "NAN". Sorry
> about the misinformation.
>


Heh .... and it was such a well thought out and presented explanation, too
:-)

The IEEE 'NaN' has apparently been implemented in perl 5.8 (not 5.6) - but
only on platforms that support it, according to 'perldoc perlop'.
On such platforms, the statement:

if(NaN != NaN) {print "NaN supported"}

should produce "NaN supported".

Or, to quote it as it appears in perlop:

perl -le '$a = NaN; print "No NaN support here" if $a == $a'
perl -le '$a = NaN; print "NaN support here" if $a != $a'

I suspect that the inconsistent behaviour the op has reported is happening
on a platform that doesn't support NaN. I'm getting the same inconsistent
behaviour, and, according to the above test(s), my platform (Win32) doesn't
support NaN. I also tried on Linux, and got the same lack of support.

This doesn't seem right to me. If the platform doesn't support 'NaN', then
'NaN' should be just another string - or at least a warning should be issued
when 'NaN' is used in a numeric context on a platform that doesn't support
NaN. The porters should be informed. Who wants to do it ? (I will, if no-one
else wants to.)

Cheers,
Rob



ivowel@gmail.com 07-24-2005 02:46 PM

Re: IEEE NaN screwed up?
 

please do. I have no idea how to reach them.

regards,

/iaw


attn.steven.kuo@gmail.com 07-24-2005 04:45 PM

Re: IEEE NaN screwed up?
 
Sisyphus wrote:
> <attn.steven.kuo@gmail.com> wrote in message
> news:1122138001.481423.193540@g43g2000cwa.googlegr oups.com...



[ snipped my well intended but faulty reasoning ]


>
> Heh .... and it was such a well thought out and presented explanation, too
> :-)




For some reason I thought the sequence:

$v = "NAN";
$v + 1.0;

would affect the scalar in the same way as:

use Scalar::Util qw(dualvar);
$v = dualvar 0, "NAN";

DWIM only goes so far, I guess.


[ more snipped ]

> I suspect that the inconsistent behaviour the op has reported is happening
> on a platform that doesn't support NaN. I'm getting the same inconsistent
> behaviour, and, according to the above test(s), my platform (Win32) doesn't
> support NaN. I also tried on Linux, and got the same lack of support.
>
> This doesn't seem right to me. If the platform doesn't support 'NaN', then
> 'NaN' should be just another string - or at least a warning should be issued
> when 'NaN' is used in a numeric context on a platform that doesn't support
> NaN. The porters should be informed. Who wants to do it ? (I will, if no-one
> else wants to.)



Seems like "INF" has the same behavior:

[sunnydale:~] skuo% perl -wle 'print "INF" + 1.0;'
1

[sunnydale:~] skuo% perl -wle 'print "foo" + 1.0;'
Argument "foo" isn't numeric in addition (+) at -e line 1.
1

[sunnydale:~] skuo% perl -v

This is perl, v5.8.6 built for darwin

Please do follow up with the P5P.

--
Thanks and regards,
Steven


Sisyphus 07-25-2005 12:27 AM

Re: IEEE NaN screwed up?
 

<ivowel@gmail.com> wrote in message
news:1122216404.317969.125110@g14g2000cwa.googlegr oups.com...
>
> please do. I have no idea how to reach them.
>


See http://lists.cpan.org/showlist.cgi?name=perl5-porters

That page contains a link to the Archive, where the thread I have just now
started (called "NaN on platforms that don't support it") can be viewed -
though I don't know how long it takes for posts to appear on the archive.

Cheers,
Rob



Tassilo v. Parseval 07-25-2005 05:24 AM

Re: IEEE NaN screwed up?
 
Also sprach Sisyphus:

><ivowel@gmail.com> wrote in message
> news:1122216404.317969.125110@g14g2000cwa.googlegr oups.com...
>>
>> please do. I have no idea how to reach them.
>>

>
> See http://lists.cpan.org/showlist.cgi?name=perl5-porters
>
> That page contains a link to the Archive, where the thread I have just now
> started (called "NaN on platforms that don't support it") can be viewed -
> though I don't know how long it takes for posts to appear on the archive.


It's often easier to simply use the 'perlbug' program that ships with
every perl. That way the report also gets assigned a bug ID and is
entered into the database of open issues.

Tassilo
--
use bigint;
$n=71423350343770280161397026330337371139054411854 220053437565440;
$m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($ m+=8)<=200);


All times are GMT. The time now is 02:20 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57