How to calculate the log of a number using bc?
Asked Answered
L

8

70

I want to calculate the log (base 10) of a number. How can I do this using bc?

Liberate answered 1/11, 2011 at 3:50 Comment(2)
They should've just called it 'log' or 'ln'. Who expects 'l'?Vizcacha
@Vizcacha all the basic functions included in the math library (bc -l) have single-character names. The cosine function is c. It's a feature ;)Scanty
I
100

Invoke bc with the -l option (to enable the math library) like so:

$ echo 'l(100)/l(10)' | bc -l
2.00000000000000000000

Use the l function which is the natural log. Take the natural log of the number you are interested in then divide it by the natural log of 10.

Indelicate answered 1/11, 2011 at 3:54 Comment(4)
log_b(x) = log_k(x) / log_k(b) :-) the logarithm in respect to base b can be computed given any logarithm function to a arbitrary base kLimicolous
Not to be picky but you should really use bc -l <<<l(100)/l(10), at least you don't worry about quoting.Kudos
no, the single-quoted variant is correct (echo '...'); with "here strings" (<<<) do you need to worry about quoting ("here strings...undergo brace expansion, tilde expansion, parameter and variable expansion, command substitution, arithmetic expansion, and quote removal.") gnu.org/software/bash/manual/html_node/…Myoglobin
In order to scale use scale=2;echo 'l(100)/l(10)' | bc -l or even better combine it with @yegle's commentCorneous
L
54

the logarithm of x in respect to base b can be computed given any logarithm function to an arbitrary base k -- that's actually pretty cool!

log_b(x) = log_k(x) / log_k(b)

e.g.

log_b(x) = ln(x) / ln(b)

if b=10:

log_10(x) = ln(x) / ln(10)

and -l in bc enables the math library

so that's why this works:

# bc -l
l(100) / l(10)
2.00000000000000000000
Limicolous answered 1/11, 2011 at 4:3 Comment(1)
Yeah I wasn't very clear on that... I just didn't know the name of the function in bc. I know how to calculate it for different bases :)Vizcacha
U
6

If you start bc with the -l switch, then there's a function l() that calculates the natural log of its argument.

Ube answered 1/11, 2011 at 3:56 Comment(0)
P
2

bc does not directly gives the logarithm in other than except or in other ways, using bc, we can calculate only ln(x). So, to get any other base logarithm, it is wise to remember some identities. The basic one is -

log base 10 (x) = log base e (x) / log base e (10)

Here I am giving you some examples of different base logarithms -

totan@Home-Computer ~ $ bc -l
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. 

l(8)/l(2)
3.00000000000000000002

l(1000)/l(10)
3.00000000000000000000

l(100000)/l(100)
2.50000000000000000000

l(100000)/l(10)
5.00000000000000000001

l(81)/l(3)
4.00000000000000000001

l(64)/l(4)
3.00000000000000000000

quit()
totan@Home-Computer ~ $ 

Hope this helps you.

Phonologist answered 14/7, 2017 at 16:35 Comment(1)
nicely copied from my answer..Limicolous
J
1
bc -l

l(____) 

(fill in the blank there with your number)

I found tutorials at:

http://unix-simple.blogspot.com/2006/10/unix-basic-calculator.html

and

http://unix-simple.blogspot.com/2006/10/taking-roots-with-unix-basic.html

Jorum answered 1/11, 2011 at 3:54 Comment(3)
log() option does not work. I get a 'function log not defined error'Liberate
yep. I mistyped "log" for "l" in my excited hurry to get an answer in before the stampede.Jorum
This is log base e, OP specifically asked for base 10.Loathly
F
1

Poster specifically requested log 10.

I have bc 1.06.95 on Ubuntu 12.10. "l(x)" in this version of BC is not base 10. It is natural log (base e). This is confirmed here and BC has worked this way since some time:

http://linux.about.com/od/commands/l/blcmdl1_bc.htm

I suspect what you need to make log base 10 work is the BC extension:

http://x-bc.sourceforge.net/extensions_bc.html

Not sure correct way to install this, and got errors trying to post it here.

Dominic-Luc Webb

Fishbein answered 25/8, 2013 at 8:52 Comment(1)
The previous answers were pretty clear about it being natural log, which is the normal default on calculators, etc. If you look at that extensions file the log() function just does exactly what was suggested here, return(l(x)/l(10)).Flattop
A
1

As many others have pointed out, bc invoked with the -l option will include the standard math libraries, and offer their functionality through extensions. @dee pointed to additional extensions that solved the problem I had of calculating log2() that led me to this post. I've found no way to "install" them, but they can be loaded at runtime by including the file when invoking bc:

bc -l extensions.bc

If you want to make this the default behavior, define the environment variable BC_ENV_ARGS to -l /pathToExtension/extensions.bc in your shell's profile or .rc

Antonietta answered 4/3, 2020 at 18:51 Comment(0)
B
0

Since bc -l only gives you the natural logarithm you can define the log function quickly yourself:

define log(x) { return l(x)/l(10) }
log(10)
1.0000000
Broddie answered 26/2, 2023 at 15:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.