Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the logarithm of a number in Scala

Tags:

scala

How do you find the base y logarithm of a number x in Scala? I have searched the scala.math library and I can't find a way. It seems to have only log10(x) and ln(x).

like image 755
Adam Avatar asked Apr 11 '15 01:04

Adam


2 Answers

This is a maths question :)

log<base y>(x) == log10(x)/log10(y) == ln(x)/ln(y)

Random link from the web that explains this:

http://www.purplemath.com/modules/logrules5.htm

like image 165
Ajay Padala Avatar answered Sep 23 '22 14:09

Ajay Padala


For convenience, you can use a lambda function, e.g.,

scala> var log2 = (x: Double) => log10(x)/log10(2.0)
log2: Double => Double = <function1>

scala> log2(2)
res0: Double = 1.0
like image 21
Jorge Avatar answered Sep 19 '22 14:09

Jorge