Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Today I've written a little program that classifies and counts files based on the integer part of log2($file_size). So I would get these classes:
0 <= SIZE < 1 1 <= SIZE < 2 2 <= SIZE < 4 4 <= SIZE < 8 and so on...
For that I would like to write this:
$size_class = $size ? int( log($size) / log(2) ) : -1; # red light on, possible error caused by floating point arithmetic
Now let's forget about the special case of $size == 0. My input can only be positive integers (because these are file sizes), so I suppose the error coming from floating point arithmetic can bite me only in the case when $size is a power of 2 (in the expression $size = 2**$x the exponent is a natural number). Is this true?

But let's take it further. I've written a little test to check whether I will be bitten for all the powers of 2 that fits in my machine's floating point represantation:

bash $ diff <( perl -E 'say int( log(2**$_) / log(2) ) for 1 .. 2**10' + ) <( seq 1 $(( 2**10 )) )
The output is this:
1024c1024 < inf --- > 1024
So there is not a single power of 2 (under 2**1024) that would be misclassified at least on my machine.

So my question is: Is there a theoretical explanation (for this restricted case of floating point aritmetic) why it can't be problematic or is it just by accident?

Please shed some light on this issue.

ps: Sorry, I know this is probably not Perl specific, but my question arose because there is no log2() function in Perl. (For example with a properly setup lookup table I could be able to do this classification with integer arithmetic only, but that would be a pain instead of this little snippet.)

In reply to May I be bitten by floating point arithmetic in the following restricted case? by rubasov

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (9)
    As of 2014-09-01 12:53 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite cookbook is:










      Results (9 votes), past polls