http://www.perlmonks.org?node_id=1010277


in reply to log() and int() problem

log returns a floating-point number, so the results are prone to floating-point inaccuracies:

$ perl -wE 'printf "%.20f\n", log(125)/log(5)' 3.00000000000000044409

A safer test is something like this here, which only uses integers:

use strict; use warnings; use 5.010; sub is_power_of { my ($num, $base) = @_; while ($num > 1) { return 0 if $num % $base != 0; $num /= $base; } return 1; } say is_power_of(125, 5);

Replies are listed 'Best First'.
Re^2: log() and int() problem
by Anonymous Monk on Dec 25, 2012 at 21:35 UTC
    Idly curious why a loop and "/=" are needed. Not questioning but curious. Example?
      Add  print "$num\n"; and you can see how the loop works