How do I calculate the log of a number using bc?
This is the first time I am using bc. I want to calculate the log (base 10) of a number. How do I this?
Solution 1:
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.
Solution 2:
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