<?xml version="1.0" encoding="windows-1252"?>
<node id="1010277" title="Re: log() and int() problem" created="2012-12-25 12:02:35" updated="2012-12-25 12:02:35">
<type id="11">
note</type>
<author id="616540">
moritz</author>
<data>
<field name="doctext">
&lt;p&gt;[doc://log] returns a floating-point number, so the results are prone to [http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html|floating-point inaccuracies]:

&lt;code&gt;
$ perl -wE 'printf "%.20f\n", log(125)/log(5)'
3.00000000000000044409
&lt;/code&gt;

&lt;p&gt;A safer test is something like this here, which only uses integers:

&lt;code&gt;
use strict;
use warnings;
use 5.010;

sub is_power_of {
    my ($num, $base) = @_;

    while ($num &gt; 1) {
        return 0 if $num % $base != 0;
        $num /= $base;
    }
    return 1;
}

say is_power_of(125, 5);
&lt;/code&gt;

&lt;!-- Node text goes above. Div tags should contain sig only --&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-616540"&gt;
[http://perl6.org/|Perl 6 - the future is here, just unevenly distributed]
&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
1010275</field>
<field name="parent_node">
1010275</field>
</data>
</node>
