perlquestion
rubasov
Today I've written a little program that classifies and counts files based on the integer part of <code>log2($file_size)</code>. So I would get these classes:
<code>
0 <= SIZE < 1
1 <= SIZE < 2
2 <= SIZE < 4
4 <= SIZE < 8
and so on...
</code>
For that I would like to write this:
<code>
$size_class = $size ? int( log($size) / log(2) ) : -1;
# red light on, possible error caused by floating point arithmetic
</code>
Now let's forget about the special case of <code>$size == 0</code>. 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 <code>$size</code> is a power of 2 (in the expression <code>$size = 2**$x</code> the exponent is a natural number). Is this true?
<p>
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:
<code>
bash $ diff <( perl -E 'say int( log(2**$_) / log(2) ) for 1 .. 2**10' ) <( seq 1 $(( 2**10 )) )
</code>
The output is this:
<code>
1024c1024
< inf
---
> 1024
</code>
So there is not a single power of 2 (under 2**1024) that would be misclassified at least on my machine.
</p>
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?
<p>
Please shed some light on this issue.
</p>
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.)