Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

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);


Comment on Re: log() and int() problem
Select or Download Code
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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1010277]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2014-07-26 12:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (176 votes), past polls