Hello Perl Monks -

I am a new user to this site - but a professional programmer for many years - hope I can be a vital contributor to your esteemed collection of knowledge.

I have a simple math question.

I recently stumbled upon a very obscure problem in one of my apps.

According to Perl,

100.10 - 100.00 = 0.0999999999999943

and not ".10" as I would have imagined from my early school days.

Try it yourself in your favorite shell (this happens in v5.83):

perl -e "print (100.10-100);"

Can someone with supreme wisdom please explain to me why perl comes to this result from simple math with a non-whole number. This is causing thousands of calculations in my app to be off by .01 - .02 - but when money is involved it needs to be 100% correct.

Is this a perl bug? Or am I missing something completely obvious?

Look forward to your replies.

In reply to Math 101 anyone? by smcone

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.