Hello, whilst putting together a reporting page I found that some of my numbers were wrong - I was getting 99% even though it should have been 100%. Prodding around and writing some test scripts I found that it was related to floating point division and sprintf rounding down. This code demonstrates some oddness:

foreach my $int ( 1 .. 600 ) { my $number = $int / $int; next if $number == 1; warn "$int / $int != 1 (== $number)\n"; } produces: 49 / 49 != 1 (== 1) 98 / 98 != 1 (== 1) 103 / 103 != 1 (== 1) 107 / 107 != 1 (== 1) 161 / 161 != 1 (== 1) 187 / 187 != 1 (== 1) 196 / 196 != 1 (== 1) 197 / 197 != 1 (== 1) 206 / 206 != 1 (== 1) 214 / 214 != 1 (== 1) 237 / 237 != 1 (== 1) ...

Now I know that $num / $num != 1 sometimes because of floating point considerations - but I am keen to know more. For instance why is $number always displayed as '1'? Also why do the hits seem to happen more often as the numbers get larger ( twice for 1..100, eleven times for 401..500)?


In reply to Insights sought into floating point division strangeness by EvdB

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.