What is Math.log10() in Scala?

The log10() function uses the base 10 to calculate the logarithm of a number.

Figure 1 shows the mathematical representation of the log10() function and the corresponding representation in Scala.

Figure 1: Mathematical representation of the log10() function and the corresponding representation in Scala

The following module is required for this function:

import scala.math._

Syntax

Double log(Double number)

Parameter

This function requires a number whose logarithm base 10 is to be calculated, and it must be greater than zero.

Return value

log10() uses the base 10 to return the logarithm of a number.

  • If the parameter value is positive infinity, then it returns positive infinity.
  • If the parameter value is NaN, less than zero, or negative infinity, then it returns NaN.
  • If the parameter value is zero, then it returns negative infinity.

Example

The following example shows how we can use the log10() function in Scala.

import scala.math._
object Main extends App {
//positive number
println(s"The value of log10(10) = ${log10(10)}");
println(s"The value of log10(2) = ${log10(2)}");
//error outputs
println(s"The value of log10(Double.PositiveInfinity) = ${log10(Double.PositiveInfinity)}");
println(s"The value of log10(Double.NegativeInfinity) = ${log10(Double.NegativeInfinity)}");
println(s"The value of log10(Double.NaN) = ${log10(Double.NaN)}");
println(s"The value of log10(-1) = ${log10(-1)}");
println(s"The value of log10(0) = ${log10(0)}");
}

Free Resources