Scala Tutorial Scala References

Scala - Math log10() Method



The Scala Math log10() method returns the base-10 logarithm 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.
  • If the argument is equal to 10n for integer n, then the result is n.

Syntax

def log10(x: Double): Double = java.lang.Math.log10(x)

Parameters

x Specify the number.

Return Value

Returns the base-10 logarithm of a given number.

Exception

NA.

Example:

In the example below, log10() method is used to calculate the base-10 logarithm of a given number.

import scala.math._

object MainObject {
  def main(args: Array[String]) {
    println(s"log10(10) = ${log10(10)}");  
    println(s"log10(100) = ${log10(100)}"); 
    println(s"log10(50) = ${log10(50)}"); 
    println(s"log10(0) = ${log10(0)}");
    println(s"log10(Double.NaN) = ${log10(Double.NaN)}"); 
    println("log10(Double.PositiveInfinity) = "
            + log10(Double.PositiveInfinity));
    println("log10(Double.NegativeInfinity) = "
            + log10(Double.NegativeInfinity));            
  }
}

The output of the above code will be:

log10(10) = 1.0
log10(100) = 2.0
log10(50) = 1.6989700043360187
log10(0) = -Infinity
log10(Double.NaN) = NaN
log10(Double.PositiveInfinity) = Infinity
log10(Double.NegativeInfinity) = NaN

❮ Scala - Math Methods