From: sreservoir on 28 Mar 2010 13:31 % perl -E'say -0' 0 % perl -E'say -undef' -0 % perl -E'say(("-0")? 1 : 0)' 1 % perl -E'say((-undef)? 1 : 0)' 0 % perl -E'say ref -undef' what is a -0 supposed to be and why does undef evaluate to it? it is apparently not the string "-0", nor is it a ref with that string value; is this some sort of magical trickery? -- "Six by nine. Forty two." "That's it. That's all there is." "I always thought something was fundamentally wrong with the universe." From: John W. Krahn on 28 Mar 2010 14:29 sreservoir wrote:> % perl -E'say -0' > 0 > % perl -E'say -undef' > -0 > % perl -E'say(("-0")? 1 : 0)' > 1 > % perl -E'say((-undef)? 1 : 0)' > 0 > % perl -E'say ref -undef' > > > what is a -0 supposed to be and why does undef evaluate to it? it is > apparently not the string "-0", nor is it a ref with that string value; > is this some sort of magical trickery? perldoc perlop [ SNIP ] Symbolic Unary Operators Unary "!" performs logical negation, i.e., "not". See also "not" for a lower precedence version of this. Unary "-" performs arithmetic negation if the operand is numeric. ^^^^^^^^^^^^^^^^^^^^^^^^^ [ *undef is not numeric.* ] If the operand is an identifier, a string consisting of a minus sign concatenated with the identifier is returned. Otherwise, if the string starts with a plus or minus, a string starting with the opposite sign is returned. One effect of these rules is that -bareword is equivalent to the string "-bareword". 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. If the string cannot be cleanly converted to a numeric, Perl will give the warning Argument "the string" isn�t numeric in negation (-) at .... John -- The programmer is fighting against the two most destructive forces in the universe: entropy and human stupidity. -- Damian Conway From: sreservoir on 28 Mar 2010 16:37 On 3/28/2010 2:29 PM, John W. Krahn wrote:> sreservoir wrote: >> % perl -E'say -0' >> 0 >> % perl -E'say -undef' >> -0 >> % perl -E'say(("-0")? 1 : 0)' >> 1 >> % perl -E'say((-undef)? 1 : 0)' >> 0 >> % perl -E'say ref -undef' >> >> >> what is a -0 supposed to be and why does undef evaluate to it? it is >> apparently not the string "-0", nor is it a ref with that string value; >> is this some sort of magical trickery? > > perldoc perlop > [ SNIP ] > Symbolic Unary Operators > Unary "!" performs logical negation, i.e., "not". See also "not" > for a lower precedence version of this. > > Unary "-" performs arithmetic negation if the operand is numeric. > ^^^^^^^^^^^^^^^^^^^^^^^^^ > [ *undef is not numeric.* ] > > If the operand is an identifier, a string consisting of a minus > sign concatenated with the identifier is returned. Otherwise, if > the string starts with a plus or minus, a string starting with > the opposite sign is returned. One effect of these rules is that > -bareword is equivalent to the string "-bareword". 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. If the string cannot be > cleanly converted to a numeric, Perl will give the warning > Argument "the string" isn�t numeric in negation (-) at .... you would think that would mean -'' would be '-'. but no, it's also -0. you're kind of making my point. -- "Six by nine. Forty two." "That's it. That's all there is." "I always thought something was fundamentally wrong with the universe." From: Ben Morrow on 28 Mar 2010 17:56 Quoth sreservoir :> % perl -E'say -0' > 0 > % perl -E'say -undef' > -0 > % perl -E'say(("-0")? 1 : 0)' > 1 > % perl -E'say((-undef)? 1 : 0)' > 0 > % perl -E'say ref -undef' > > > what is a -0 supposed to be and why does undef evaluate to it? it is > apparently not the string "-0", nor is it a ref with that string value; > is this some sort of magical trickery? -0 is 'negative zero'. It's an IEEE floating-point concept, used when you evaluate something like (-1)/(2^100) and you haven't got enough precision to represent the result but it's still important to know it's negative. Perl is a little funny about -0: C<-0> gives C<0>, since integers don't have a concept of -0, but C<-0.0> gives C<-0>, since floats do. (This is arguably a bug: the internal numeric representation shouldn't leak out like that.) For some reason, undef gets numified as a floating-point zero rather than an integral zero, so it can be negated. Ben From: Ben Morrow on 29 Mar 2010 22:34 Quoth Shmuel (Seymour J.) Metz :> In , on 03/28/2010 > at 10:56 PM, Ben Morrow said: > > >-0 is 'negative zero'. It's an IEEE floating-point concept, > > It's much older than that. There were sign-magnitude and ones complement > machines in to 1950s, for which -0 is a well defined value. For that > matter, the decimal computers also had negative 0. OK, fair enough :). I'm not that old... In Perl's case, though, -0 is supported because it's implemented on top of IEEE FP. Ben