Just like there are periodic numbers in decimal (1/3), there are periodic numbers in binary (1/10). To store those numbers as a decimal number (like floats do) would require infinite storage, so some error is introduced into those numbers when storing them in floats. I suspect your problem lies such those numbers. Here are two examples
>perl -le"print substr +(reverse unpack 'B*', pack 'd', shift), 12" .1
1101100110011001100110011001100110011001100101011001
^^^^ period
>perl -le"printf '%.16e', shift" .1
1.0000000000000001e-001
^ error
The error isn't always obvious at first since the system does a little bit of rounding automatically, but some errors can easily be noticed when the error is compounded through addition.
>perl -le"$x=.1; $n=$x*100; print int $n"
10
>perl -le"$x=.1; $n+=$x for 1..100; print int $n"
9
Note: reverse is needed on x86 machines since they store floats in little-endian order.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.