Kotlin cos()

_alexvo's avatar
Published Nov 13, 2023
Contribute to Docs

The math.cos() method in Kotlin’s math class is used to calculate the cosine of an angle in radians.

  • 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

Syntax

val cosineValue = cos(angle)
  • angle: The angle in radians for which to calculate the cosine.
  • The math.cos() method returns a value of type Double representing the cosine of the input angle.

Example

The example below demonstrates how to use math.cos() method to calculate the cosine of an angle:

import kotlin.math.*
fun main() {
val angle = PI / 3.0 // 60 degrees in radians
val cosineValue = cos(angle)
println("The cosine of $angle radians is $cosineValue.")
}

This results in the following output:

The cosine of 1.0471975511965979 radians is 0.5000000000000001.

All contributors

Contribute to 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