Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Strange log behavior?

by Anonymous Monk
on Sep 07, 2013 at 16:33 UTC ( #1052825=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Maybe I am missing something very obvious, but why is the following behavior going on in Perl (5.14.2)?
3 % 3 # returns 0 log (1000) / log (10) # returns 3 (log(1000) / log(10)) % 3 # returns 3

Comment on Strange log behavior?
Download Code
Re: Strange log behavior?
by ikegami (Pope) on Sep 07, 2013 at 16:42 UTC
    >perl -E"say sprintf '%.20g', log (1000) / log (10)" 2.9999999999999996 >perl -E"say int( log (1000) / log (10) )" 2 >perl -E"say log (1000) / log (10) % 3" 2 >perl -E"say 2 % 3" 2

    Always use a tolerance when comparing floating point numbers or converting them to integers.

      What is the correct way to round the floating point number to 3?

        sprintf seems to do the right thing.

        perl -E ' say sprintf q{%.0f}, $_ for 2.99999, 2.49999, 3, -2.99999, -2.49999, 5279.8012;' 3 2 3 -3 -2 5280 $

        I hope this is helpful.

        Cheers,

        JohnGG

Re: Strange log behavior?
by Eily (Chaplain) on Sep 07, 2013 at 16:51 UTC

    You probably wrote something like : print (log(1000) / log(10)) % 3 Which, has stated in perlop, actually means (print (log(1000 / log(10))) % 3.

    So you print (log 100)/(log 10) (and the result of this operation is rounded to 3), and then do a modulo operation on the output of print, which doesn't make much sense.

    Edit: if that is what happened, use warnings would have given you the answer

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1052825]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2014-10-25 19:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (148 votes), past polls