Scala - Math log() Method
The Scala Math log() method returns the natural logarithm (base e) of a given number. In special cases it returns the following:
- If the argument is NaN or less than zero, then the result is NaN.
- If the argument is positive infinity, then the result is positive infinity.
- If the argument is zero, then the result is negative infinity.
Syntax
def log(x: Double): Double = java.lang.Math.log(x)
Parameters
x |
Specify the number. |
Return Value
Returns the natural logarithm of a given number.
Exception
NA.
Example:
In the example below, log() method is used to calculate the natural logarithm of a given number.
import scala.math._ object MainObject { def main(args: Array[String]) { println(s"log(E) = ${log(E)}"); println(s"log(10) = ${log(10)}"); println(s"log(50) = ${log(50)}"); println(s"log(0) = ${log(0)}"); println(s"log(Double.NaN) = ${log(Double.NaN)}"); println("log(Double.PositiveInfinity) = " + log(Double.PositiveInfinity)); println("log(Double.NegativeInfinity) = " + log(Double.NegativeInfinity)); } }
The output of the above code will be:
log(E) = 1.0 log(10) = 2.302585092994046 log(50) = 3.912023005428146 log(0) = -Infinity log(Double.NaN) = NaN log(Double.PositiveInfinity) = Infinity log(Double.NegativeInfinity) = NaN
❮ Scala - Math Methods