Yeah, this is a pretty stupid feature. No, I'm not at all surprised that floating point isn't always precise. Yes, I find it quite stupid and very annoying to have almost half of my calculations that result in "zero" being displayed as "-0.00" due to trivia like single-bit errors that are impossible to avoid. This should just be fixed (yes, the fact that C got this wrong shouldn't prevent Perl from being better).

printf displaying "0.02" means that the number I asked to have displayed is closer to "0.02" than it is to "0.01" or to "0.03"1. I don't need some special indicator displayed to tell me that the number might actually be less than "0.02" even though it is displayed as "0.02".

1 Some of you might be thinking something like "or exactly halfway between". But if you are, then you are wrong because neither 0.015 nor 0.025 can be represented in (base-2) floating point.

Now repeat the above paragraph (and footnote) with 0.02 subtracted from all of the example numbers. That logic still makes perfect sense. One would be a fool to see "0.00" and declare "This means that the number cannot be between -0.005 and 0.00; it must be a positive number!". Now, I could see having "%+.2f" display either "+0.00" or "-0.00" since at least "+0.00" could be considered misleading in the case of -0.000001. I could also see "%+.2f" displaying just "0.00" since zero isn't a positive number (and that'd be my preference, actually).

Just a few days ago I was doing some calculations where I thought some totals given to me had had fees subtracted from them. So I wrote a quick Perl script to parse out the columns of numbers and do some quick multiplication and subtraction and display the results. I got:

0.00 -0.00 -0.00 0.00 0.00 0.00 -0.00

The fees hadn't been taken out. And that is just stupid output. Who would want to know that 3 of the calculations were ever so slightly inaccurate to one side and not care in the least that a bunch of the other calculation were ever so slightly inaccurate to the other side?!

Entering the same data into Excel results in only "0.00" results. For those who can't believe that MicroSoft would ever do anything right, the same is true for Google spreadsheets. I couldn't find the spreadsheet app in my OpenOffice installation to verify it.

(No, I don't actually expect sanity to be restored here. But I'm glad to have made the case.)

- tye        


In reply to Re: Why is Zero not 0? (yuck) by tye
in thread Why is Zero not 0? by jluther

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.