Perl / perl5

🐪 The Perl programming language
https://dev.perl.org/perl5/
Other
1.97k stars 560 forks source link

Re: Bug with vec() and bitwise OR? #1952

Closed p5pRT closed 21 years ago

p5pRT commented 24 years ago

Migrated from rt.perl.org#3223 (status was 'resolved')

Searchable as RT3223$

p5pRT commented 24 years ago

From rmb1@cise.npl.co.uk

Hey all\,

The following snippet just appeared on c.l.p.misc\, and I think it unveils a bug (at least on 5.005_03)​:

\#\!/usr/bin/perl \-w

use strict;
my $x = my $y = my $z = 0;

vec\($x\, 0\, 1\) = 1;
vec\($y\, 1\, 1\) = 1;

$z = $x | $y;

print "$x $y $z\\n";
\_\_END\_\_

The above prints​:

1 2 0

The values of $x and $y are fine\, but $z should be 3\, no? If you explicitly set $x to 1 and $y to 2 (without using vec())\, then you get the expected result for $z.

Anybody with a good explanation? I don't have 5.6 handy. Anybody can try it there?

% perl -we 'for (qw(0 "0" "@​" "A" "")\, q(" ")) { my $x = eval $_; die if $@​; vec($x\,0\,1)=1; print qq($_ => "$x"\, )\, ($x == 1 ? "yes" : "no")\, "\n"; }' 0 => "1"\, no "0" => "1"\, yes Argument "A" isn't numeric in eq at -e line 1. "@​" => "A"\, no Argument "A" isn't numeric in eq at -e line 1. "A" => "A"\, no Argument "^A" isn't numeric in eq at -e line 1. "" => ""\, no Argument "!" isn't numeric in eq at -e line 1. " " => "!"\, no

It appears that after C\<$x = 0; vec($x\,0\,1)=1;>\,   $x is numeric (no warning)   is not numerically equal to 1\,   but is the string "1".

Robin

p5pRT commented 24 years ago

From [Unknown Contact. See original ticket]

I have a patch for this​:

Hey all\,

The following snippet just appeared on c.l.p.misc\, and I think it unveils a bug (at least on 5.005_03)​:

\#\!/usr/bin/perl \-w

use strict;
my $x = my $y = my $z = 0;

vec\($x\, 0\, 1\) = 1;
vec\($y\, 1\, 1\) = 1;

$z = $x | $y;

print "$x $y $z\\n";
\_\_END\_\_

The above prints​:

1 2 0

The values of $x and $y are fine\, but $z should be 3\, no? If you explicitly set $x to 1 and $y to 2 (without using vec())\, then you get the expected result for $z.

Anybody with a good explanation? I don't have 5.6 handy. Anybody can try it there?

% perl -we 'for (qw(0 "0" "@​" "A" "")\, q(" ")) { my $x = eval $_; die if $@​; vec($x\,0\,1)=1; print qq($_ => "$x"\, )\, ($x == 1 ? "yes" : "no")\, "\n"; }' 0 => "1"\, no "0" => "1"\, yes Argument "A" isn't numeric in eq at -e line 1. "@​" => "A"\, no Argument "A" isn't numeric in eq at -e line 1. "A" => "A"\, no Argument "^A" isn't numeric in eq at -e line 1. "" => ""\, no Argument "!" isn't numeric in eq at -e line 1. " " => "!"\, no

It appears that after C\<$x = 0; vec($x\,0\,1)=1;>\, $x is numeric (no warning) is not numerically equal to 1\, but is the string "1".

Robin

Perl-Friends​: To unsubscribe\, mail \perl\-friends\-unsubscribe@&#8203;perlsupport\.com

With my patch the original script prints "1 2 3" and my script print "yes".

Robin

--- doop.c 2000/05/11 12​:55​:39 1.1 +++ doop.c 2000/05/11 13​:30​:41 1.2 @​@​ -897\,6 +897\,7 @​@​   } #endif   } + SvNIOK_off(targ); /* see t/op/vec.t test 16 */   SvSETMAGIC(targ); }

Patch​: --- t/op/vec.t 2000/05/11 13​:18​:33 1.1 +++ t/op/vec.t 2000/05/11 13​:30​:41 1.2 @​@​ -4\,5 +4\,5 @​@​

-print "1..15\n"; +print "1..16\n";

print vec($foo\,0\,1) == 0 ? "ok 1\n" : "not ok 1\n"; print length($foo) == 0 ? "ok 2\n" : "not ok 2\n"; @​@​ -24\,4 +24\,6 @​@​ vec($Vec\, 0\, 32) = 0xbaddacab; print $Vec eq "\xba\xdd\xac\xab" ? "ok 14\n" : "not ok 14\n"; print vec($Vec\, 0\, 32) == 3135089835 ? "ok 15\n" : "not ok 15\n"; +vec($foo = 0\,0\,1) = 1; # ensure $foo forgets its original numerical value +print $foo == 1 ? "ok 16\n" : "not ok 16\n";

End of patch

p5pRT commented 24 years ago

From @tamias

On Thu\, May 11\, 2000 at 03​:35​:41PM +0100\, Robin Barker wrote​:

--- doop.c 2000/05/11 12​:55​:39 1.1 +++ doop.c 2000/05/11 13​:30​:41 1.2 @​@​ -897\,6 +897\,7 @​@​ } #endif } + SvNIOK_off(targ); /* see t/op/vec.t test 16 */

That comment won't make sense if the tests are changed around. It would be better to put a real comment here.

 SvSETMAGIC\(targ\);

}

Ronald

p5pRT commented 21 years ago

From rmb1@cise.npl.co.uk

Fixed by patch supplied by Robin Barker\, the additional test (t/op/vec.t #16) is still in the test suite.

p5pRT commented 21 years ago

rmb1@cise.npl.co.uk - Status changed from 'open' to 'resolved'