Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re: log() and int() problem

by moritz (Cardinal)
on Dec 25, 2012 at 17:02 UTC ( #1010277=note: print w/replies, xml ) Need Help??

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1010277]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (11)
As of 2018-05-24 14:34 GMT
Find Nodes?
    Voting Booth?