note
Limbic~Region
[dragonchild],
<br />
I understand the point you were trying to make, but you picked a bad example.
<CODE>
print nearest_power_of_2( 50 );
sub nearest_power_of_2 {
my $x = shift;
my $n = log( $x ) / log( 2 );
return $x if $n == int $n;
my ($below, $above) = (int $n, int $n + 1);
$_ = 2 ** $_ for ($above, $below);
return $above - $x > $x - $below ? $below : $above;
}
</CODE>
<div class="pmsig"><div class="pmsig-180961">
<p>
Cheers - [Limbic~Region|L~R]
</p>
</div></div>
<small>Thanks to [blokhead] for reminding me how to convert logarithmic bases</small>
430254
430620