log()
Published Oct 18, 2023
Contribute to Docs
The log()
method in Kotlin’s math
class is used to calculate the logarithm of a number with respect to a base value.
Syntax
log(n, base)
n
: The number of typeDouble
for which the logarithm is to be calculated.base
: The number of typeDouble
that specifies the logarithm base.
The .log()
method returns a value of type Double
, the logarithm of n
to base
. The following values represent special cases:
log(n, base)
isNaN
if eithern
orbase
areNaN
.log(n, base)
isNaN
whenn < 0
orbase <= 0
orbase == 1.0
.log(+Inf, +Inf)
isNaN
.log(+Inf, base)
is+Inf
forbase > 1
and-Inf
forbase < 1
.log(0.0, base)
is-Inf
forbase > 1
and+Inf
forbase < 1
.
Example
This example shows how to use the log()
method to calculate the logarithm of 12
with base 2
:
import kotlin.math.*fun main(){println(log(12.0,2.0))}
The output of this code will be:
3.5849625007211565
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Kotlin on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn Kotlin
Learn Kotlin, the expressive, open-source programming language developed by JetBrains.Beginner Friendly9 hours