Kotlin 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 typeDoublefor which the logarithm is to be calculated.base: The number of typeDoublethat 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)isNaNif eithernorbaseareNaN.log(n, base)isNaNwhenn < 0orbase <= 0orbase == 1.0.log(+Inf, +Inf)isNaN.log(+Inf, base)is+Infforbase > 1and-Infforbase < 1.log(0.0, base)is-Infforbase > 1and+Infforbase < 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
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn Kotlin, the expressive, open-source programming language developed by JetBrains.
- Beginner Friendly.9 hours