There are several Perl-like languages that decided that string '0' should be "true". While I understand the temptation to only have one string that is "false", my experience is that this choice was a mistake (in each case) -- I have seen repeated cases of bugs due to "as a string" vs "as a number" causing opposite behavior. It would be an even bigger mistake in Perl where the transformation between strings and numbers is even more implicit.

Since you will never make the string "NaN" (or the other stringifications of NaNs) "false", I believe it would be a mistake to make a (non-string) NaN "false" in Perl.

I also disagree that a NaN should be "false" even ignoring the stringification problem. For numbers, 0 is false. NaN is very much not zero. I assume you aren't making any of the "Inf"s "false". A NaN is more like an Inf than like a zero, so I find it unnatural to draw the circle of "is false" around such different things as zeros and NaNs while having it separate NaNs from Infs.

I appreciate that a NaN is similar to undef. However, an undef becomes a zero whenever you treat it like a number and there are even ways for an undef to silently become a zero ($undefined++). So undef is much more like zero than a NaN is like zero.

So I think it is very natural for undef to be "false". And I find it natural in Perl for "uninitialized" to be "false".

So I need to address the case of an "uninitialized number". I can appreciate the value in having an uninitialized number being a NaN. And I can see the expectation that "uninitialized" would be "false" (though a bit less strongly for a strictly numeric data type since, when I test a number for "truth", I usually think of it as short-hand for comparing a number to 0). But I would be very bothered by doing a calculation like $x/$y and ending up with an "uninitialized" value, or even a false value.

So I'd probably have an uninitialized number be a special undef type of NaN, a value that could never be generated from a calculation and that stringifies to an empty string (with a warning). I very much would not have a non-zero result of a calculation be "false" (especially when it stringifies to a "true" value).

- tye        


In reply to Re: NaNs are true (!"NaN") by tye
in thread NaNs are true by syphilis

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.