38.91 is a periodic number in binary, so some truncating occurs when the computer tries to store that number as a float. That means that 38.91 * 100 is not equal to 3891.

>perl -le"printf '%.20e', 38.91 3.89099999999999970000e+001 >perl -le"printf '%.20e', 38.91 * 100 3.89099999999999950000e+003

That means you are calculating the modulus of 3890, not 3891.

Solution 1: You could round the number: (1024 is a power of two, so no error is introduced. But 1000 would be fine too since the error introduced by 1000 would be so small that it would be removed by the rounding.)

>perl -le"$n=38.91*100; $n=sprintf('%f',$n*1024)/1024; print $n%5; 1

Solution 2: Work in cents (or even smaller units) instead of dollars, so that your numbers are all integers.

>perl -le"$n=3891; print $n%5; 1

Update: Elaborated a little.


In reply to Re: Modulus strangeness (%) by ikegami
in thread Modulus strangeness (%) by Anonymous Monk

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.