Some people have told you why, but not what to do about it.
In general case of a 32bit float the float has a factor of 23bit accuracy, and since base 10 log of 2^23 = 6.9 digits, it is safe to use 5 digits for yourself, and 1 digit for error.
For example. If you know your numbers are 000.00 to 999.99 then you can simply add 0.001 before you round down (or subtract 0.001 before you round up), then you will never encounter this problem and your numbers will always have their 'ideal' value.
perl -e "my $val = 8.95; printf('%d',int(($val + 0.001) * 100));"
895
If you have 64bit float then its safe to assign 14 digits for yourself and use the 15th for error:
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.