Logarithm

Basic types / Numbers / Mathematical operations

#bash has no fractional numbers; bc -l loads the math library,
#where l() is the natural logarithm, so log base b is l(x) / l(b)
number=512
logBase=8
result=$(bc -l <<< "l($number) / l($logBase)")
#result is 3.00000000000000000000

#the last digits of bc -l are not exact, printf %g rounds them
ln=$(bc -l <<< "l(e(1))")
#ln is .99999999999999999999
printf -v lnShort '%g' "$ln"
#lnShort is 1

#awk has log() for the natural logarithm, too
result2=$(awk -v x="$number" 'BEGIN { print log(x) / log(2) }')
#result2 is 9

echo "result is $result"
echo "ln is $ln"
echo "lnShort is $lnShort"
echo "result2 is $result2"