log()

manviii_278 total contributions
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
Looking to contribute?
- 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.