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