0

Just puzzling to me.

Related, but different question: What does “0 but true” mean in Perl?

Community
  • 1
  • 1
blunders
  • 3,619
  • 10
  • 43
  • 65
  • 2
    9-9, -7+2+5, 100*0 and 10%5 all mean the same thing as in this article subject too! There are an infinite number of ways of writing zero... – tadmc Mar 24 '11 at 22:01

5 Answers5

8

Perl doesn't distinguish kinds of numbers. Looking at all of those with a non-CS/programmer eye, they all mean the same thing to me as well: zero. (This is one of the foundations of Perl: it tries to work like people, not like computers. "If it looks like a duck....")

So, if you use them as numbers, they're all the same thing. If you use them as strings, they differ. This does lead to situations where you may need to force one interpretation ("0 but true"; see also "nancy typing"). but by and large it "does the right thing" automatically.

geekosaur
  • 59,309
  • 11
  • 123
  • 114
  • @geekosaur: If is knows the right thing to do, or in this case say... why is it telling me "0e0 rows found." – blunders Mar 24 '11 at 19:02
  • That looks like a database module doing an "0 but true" thing (see the comments to that question). Alternately it's coming directly from the database manager, but that would be weird. Could you show some context? – geekosaur Mar 24 '11 at 19:05
  • @geekosaur: http://stackoverflow.com/questions/5424107/update-in-perl-dbi-not-working-why-full-source-posting-including-ddl-to-creat – blunders Mar 24 '11 at 19:14
  • Looks like DBI is indeed doing an 0-but-true. https://encrypted.google.com/search?q=mysql%20dbi%200e0&ie=utf-8&oe=utf-8 finds a fair amount of discussion. – geekosaur Mar 24 '11 at 19:17
  • @geekosaur: Yes, that Google is how I found the reference to the question, and the examples of ZERO being read as ZERO. People only generalize if they don't know. – blunders Mar 24 '11 at 19:25
  • 2
    @blunders, The string '0E0' is true, but if you treat it as a number it's equivalent to zero. It allows `execute` to indicate no errors occurred even when no rows were affected. – ikegami Mar 25 '11 at 05:20
3

I don't understand, what else should they mean?

You give integer, scientific, floating point, signed integers and octal notations of zero. Why should they differ?

musiKk
  • 14,751
  • 4
  • 55
  • 82
  • +1 @musiKk: So Perl sees them as more than ZERO, or you do? Question is why Perl does not see them as types of ZERO, I guess. Thanks breaking out the differences, since even that was not clear to me. – blunders Mar 24 '11 at 18:56
  • There are no "types" of zero.. only zero, and it represents false in perl. One exception: "0 but true". Thats all – matthias krull Mar 24 '11 at 19:35
  • @mugen kenichi: You have your exceptions reversed. The exception is the string "0", which is false (as are "", undef, and 0 (the number, not the string)). All the others are true, as is easily verified with `perl -E 'say "$_\t" . ($_ ? "true" : "false") for qw( 0 0e0 0.0 -0 +0 000)'` – Dave Sherohman Mar 25 '11 at 12:09
  • Indeed.. i slaped myself several times. – matthias krull Mar 25 '11 at 12:35
3

0==0 as everyone, including Larry Wall, knows.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
2

Perl interprets every scalar value as both a string and (potentially) a number. All of those string representations of zero can convert to the integer value 0 , according to perl's conversion rules:

"0", "0.0", "-0", "+0", "000" => Simplest case of straight string to numeric conversion. "0e0" => In a numeric context, only the leading valid numeric characters are converted, so only the leading "0" is used. For example, "1984abcdef2112" would be interpreted numerically as 1984.

"0 but true" in perl means that a string like "0e0" will evalutate numerically to 0, but in a boolean context will be "true" because the conversion to boolean follows different rules than the strict numeric conversion.

MikeH
  • 51
  • 1
  • 1
    0e0 is actually scientific notation. 5e3 is 5,000. So you could use 0e9999999 or whatever floats your boat after the e. – daotoad Mar 24 '11 at 19:22
1

Perl works in contexts. In string context, they are all different. In numeric context, they are all zero.

print "same string\n" if '0' eq '0.0';
print "same number\n" if 0 == 0.0;

'0 but true' in boolean context is true:

print "boolean context\n" if '0 but true';
print "string context\n" if '0 but true' eq '0';
print "numeric context\n" if '0 but true' == 0;
shawnhcorey
  • 3,545
  • 1
  • 15
  • 17