Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Clean log_10 ?

by Eily (Monsignor)
on Nov 28, 2019 at 16:57 UTC ( #11109378=note: print w/replies, xml ) Need Help??


in reply to Clean log_10 ?

I'm interested to get the log10 of a whole number
(IIRC) Unless that number is a power of ten, the log10 is going to be irrational. This mean you wouldn't even be able to get an exact value by using Rationals (that's the definition). Best you can do to have an "exact value" would be something like:
sub log10 { sprintf("%e", $_[0]); print "log10($_[0]) = $2 + log10($1)" }
where you can drop the right side of the addition when $1 == 1.0. Oh and the right side is guaranteed to be in [0;1)

Edit: I suppose POSIX's log10, as demonstrated by pryrt does give the correct answer for the special cases that are powers of ten. But it's still going to give an approximation for all other numbers.

Replies are listed 'Best First'.
Re^2: Clean log_10 ?
by LanX (Archbishop) on Nov 28, 2019 at 17:15 UTC
    > > I'm interested to get the log10 of a whole number

    > the log10 is going to be irrational

    Yeah you're right.

    Rewording: I need the correct int(log10) to reproduce the E notation of a whole number.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      Do you mean that the number you are taking the log of will always be a power of 10? In which case, the log will be almost an integer, so round to nearest int: $power = int($log10 + 0.5);

      Otherwise, why not use sprintf "%e", $num or similar?

      Dave.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2020-05-29 20:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (170 votes). Check out past polls.

    Notices?