Read the replies to When 1000 is less than 1000.

The short version is that floating point values can not precisely represent decimal fractions like 0.1. The result is that small errors are present whenever you perform arithmetic in computers using floating point representations of decimal fractions. Those errors are often not shown because they are small, but lead to the sort of strange results you have seen.

If you are dealing with monetary values you can avoid the problem by performing all calculations in, for example, cents then convert to dollars and cents for display. Even then, if you perform mark up, discount, interest, tax or other calculations you will have to take care to round results at an appropriate point and always compare integer cents.

True laziness is hard work

In reply to Re: Numeric Comparisons Randomly Giving the Wrong Result by GrandFather
in thread Numeric Comparisons Randomly Giving the Wrong Result by Likeless

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.