How do I calculate the log of a number using bc?

2019-02-04 00:39发布

This is the first time I am using bc. I want to calculate the log (base 10) of a number. How do I this?

标签: unix bc
7条回答
beautiful°
2楼-- · 2019-02-04 01:09

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 log of the number you are interested in then divide by the natural log of 10.

查看更多
等我变得足够好
3楼-- · 2019-02-04 01:16

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.

查看更多
贼婆χ
4楼-- · 2019-02-04 01:16

Python can come in handy for this as,

log3=$(python -c  'import math; print math.log(3)')

Hope that helps!

查看更多
相关推荐>>
5楼-- · 2019-02-04 01:25

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

查看更多
beautiful°
6楼-- · 2019-02-04 01:32

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

查看更多
登录 后发表回答