You didn't mention where your data comes from. One problem is that as soon as you import data as floating point, it is stored in an internal format that loses precision if it cannot be represented precisely as n/2^m (edited). Whatever the conversion, do it as early as possible, as the first thing you do with your data before any other mathematical operation.

MJD suggests in his blog about Moonpig: a billing system that doesn't suck that because floating-point numbers are among the things that suck, the law of the Moonpig project states that "...all money amounts are integers. Each money amount is an integral number of “millicents”..."

The blog's discussion of handling of financial numbers is very helpful. Please do read it. I don't think I would touch financial data again without first browsing through it to refresh my memory.


Dave


In reply to Re: Strange int() result by davido
in thread Strange int() result by menth0l

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.