From: Bo Lindbergh on
For real strangeness, you need to mix negative zero, boolean context,
and stringification.

{
my $strange=-0.0;
print $strange ? "$strange is true\n" : "$strange is false\n";
print $strange ? "$strange is true\n" : "$strange is false\n";
}


/Bo Lindbergh
From: Ilya Zakharevich on
On 2010-03-30, Bo Lindbergh <blgl(a)hagernas.com> wrote:
> For real strangeness, you need to mix negative zero, boolean context,
> and stringification.
>
> {
> my $strange=-0.0;
> print $strange ? "$strange is true\n" : "$strange is false\n";
> print $strange ? "$strange is true\n" : "$strange is false\n";
> }

This is a bug. Read-only access should not change the value.

Yours,
Ilya
From: Marc Girod on
On Mar 28, 10:56 pm, Ben Morrow <b...(a)morrow.me.uk> wrote:

> For some reason, undef gets numified as a floating-point
> zero rather than an integral zero, so it can be negated.

May I check that I understand this with respect to the excerpt of
perlop John gave:

$ perl -E'say undef'

$ perl -E'say q()'

$ perl -E'say -undef'
-0
$ perl -E'say -q()'
0
$ perl -E'say "-undef"'
-undef

> If the operand is an identifier, a string consisting of a minus
> sign concatenated with the identifier is returned.

So, not this case:
undef is not an 'identifier', and the return is no '-undef'.

> Otherwise, if the string starts with a plus or minus,
> a string starting with the opposite sign is returned.

No.

> One effect of these rules is that -bareword is equivalent
> to the string "-bareword".

No.

> If, however, the string begins with a non-alphabetic character
> (excluding "+" or "-"), Perl will attempt to convert the
> string to a numeric and the arithmetic negation is performed.

I understand that this is what applies: undef is a 'non-alphabetic
character'?
There, undef is converted to 'floating-point 0', whereas q() (the
empty string) is converted to 'integer 0'.

> If the string cannot be cleanly converted to a numeric,
> Perl will give the warning

No: no warning.

Thanks,
Marc
From: Bo Lindbergh on
In article <slrnhr5dup.gc4.nospam-abuse(a)powdermilk.math.berkeley.edu>,
Ilya Zakharevich <nospam-abuse(a)ilyaz.org> wrote:
> On 2010-03-30, Bo Lindbergh <blgl(a)hagernas.com> wrote:
> > For real strangeness, you need to mix negative zero, boolean context,
> > and stringification.
> >
> > {
> > my $strange=-0.0;
> > print $strange ? "$strange is true\n" : "$strange is false\n";
> > print $strange ? "$strange is true\n" : "$strange is false\n";
> > }
>
> This is a bug. Read-only access should not change the value.

Would you call this example the same bug, a different bug, or not a bug?
{
my $strange="-0";
print "- $strange == ", -$strange, "\n";
my $ignored=0+$strange;
print "- $strange == ", -$strange, "\n";
}

Ditto for this one.
{
my($one,$two)=("12","21");
print "$one | $two == ", $one | $two, "\n";
my $ignored=$one+$two;
print "$one | $two == ", $one | $two, "\n";
}


/Bo Lindbergh
From: Ben Morrow on

Quoth Bo Lindbergh <blgl(a)hagernas.com>:
> In article <slrnhr5dup.gc4.nospam-abuse(a)powdermilk.math.berkeley.edu>,
> Ilya Zakharevich <nospam-abuse(a)ilyaz.org> wrote:
> > On 2010-03-30, Bo Lindbergh <blgl(a)hagernas.com> wrote:
> > > For real strangeness, you need to mix negative zero, boolean context,
> > > and stringification.
> > >
> > > {
> > > my $strange=-0.0;
> > > print $strange ? "$strange is true\n" : "$strange is false\n";
> > > print $strange ? "$strange is true\n" : "$strange is false\n";
> > > }
> >
> > This is a bug. Read-only access should not change the value.
>
> Would you call this example the same bug, a different bug, or not a bug?
> {
> my $strange="-0";
> print "- $strange == ", -$strange, "\n";
> my $ignored=0+$strange;
> print "- $strange == ", -$strange, "\n";
> }

That's not a bug, it's documented behaviour (at least, the "+0" output
is). Compare

my $s = "-foo"; say -$s;
0 + $s; say -$s;

Admittedly it's slightly *weird* behaviour, but there you go.

It probably is a bug that "-0" numifies as integer 0 without a warning,
since that's the stringification of -0.0. This is also a bug:

my $s = 0.0; say -$s;
0 + $s; say -$s;

but the problem here is that Perl needs to realise that even though
int(-0.0) == -0.0 they are not the same number, which is a little
confusing.

> Ditto for this one.
> {
> my($one,$two)=("12","21");
> print "$one | $two == ", $one | $two, "\n";
> my $ignored=$one+$two;
> print "$one | $two == ", $one | $two, "\n";
> }

That's a different bug. It's a design bug in the bitwise ops, it's
documented, and it cannot be fixed at this point due to compatibility.

Ben