cos
Требования к платформе и версии: Common (1.2), Wasm-JS (1.8), Wasm-WASI (1.8)
expect fun cos(x: Double): Double
Вычисляет косинус угла x, заданного в радианах.
Особые случаи:
cos(NaN|+Inf|-Inf)— этоNaN
Примеры
import kotlin.math.*
import kotlin.test.*
fun main() {
//sampleStart
val epsilon = 1e-10
println(cos(0.0)) // 1.0
// Results may not be exact, so we're only checking that they are within epsilon from the expected value
// cos(π/2) = 0
println("cos(PI / 2).absoluteValue < epsilon is ${cos(PI / 2).absoluteValue < epsilon}") // true
println(cos(PI)) // -1.0
// special cases
println(cos(Double.NaN)) // NaN
println(cos(Double.POSITIVE_INFINITY)) // NaN
println(cos(Double.NEGATIVE_INFINITY)) // NaN
//sampleEnd
}
expect fun cos(x: Float): Float
Вычисляет косинус угла x, заданного в радианах.
Особые случаи:
cos(NaN|+Inf|-Inf)— этоNaN
Примеры
import kotlin.math.*
import kotlin.test.*
fun main() {
//sampleStart
val epsilon = 1e-6f
println(cos(0.0f)) // 1.0
// Results may not be exact, so we're only checking that they are within epsilon from the expected value
// cos(π/2) = 0
println("cos(PI.toFloat() / 2).absoluteValue < epsilon is ${cos(PI.toFloat() / 2).absoluteValue < epsilon}") // true
println(cos(PI.toFloat())) // -1.0
// special cases
println(cos(Float.NaN)) // NaN
println(cos(Float.POSITIVE_INFINITY)) // NaN
println(cos(Float.NEGATIVE_INFINITY)) // NaN
//sampleEnd
}
Требования к платформе и версии: JS (1.2), JVM (1.2)
actual inline fun cos(x: Double): Double
Вычисляет косинус угла x, заданного в радианах.
Особые случаи:
cos(NaN|+Inf|-Inf)— этоNaN
Примеры
import kotlin.math.*
import kotlin.test.*
fun main() {
//sampleStart
val epsilon = 1e-10
println(cos(0.0)) // 1.0
// Results may not be exact, so we're only checking that they are within epsilon from the expected value
// cos(π/2) = 0
println("cos(PI / 2).absoluteValue < epsilon is ${cos(PI / 2).absoluteValue < epsilon}") // true
println(cos(PI)) // -1.0
// special cases
println(cos(Double.NaN)) // NaN
println(cos(Double.POSITIVE_INFINITY)) // NaN
println(cos(Double.NEGATIVE_INFINITY)) // NaN
//sampleEnd
}
actual inline fun cos(x: Float): Float
Вычисляет косинус угла x, заданного в радианах.
Особые случаи:
cos(NaN|+Inf|-Inf)— этоNaN
Примеры
import kotlin.math.*
import kotlin.test.*
fun main() {
//sampleStart
val epsilon = 1e-6f
println(cos(0.0f)) // 1.0
// Results may not be exact, so we're only checking that they are within epsilon from the expected value
// cos(π/2) = 0
println("cos(PI.toFloat() / 2).absoluteValue < epsilon is ${cos(PI.toFloat() / 2).absoluteValue < epsilon}") // true
println(cos(PI.toFloat())) // -1.0
// special cases
println(cos(Float.NaN)) // NaN
println(cos(Float.POSITIVE_INFINITY)) // NaN
println(cos(Float.NEGATIVE_INFINITY)) // NaN
//sampleEnd
}
Требования к платформе и версии: Native (1.3)
actual external fun cos(x: Double): Double
Вычисляет косинус угла x, заданного в радианах.
Особые случаи:
cos(NaN|+Inf|-Inf)— этоNaN
Примеры
import kotlin.math.*
import kotlin.test.*
fun main() {
//sampleStart
val epsilon = 1e-10
println(cos(0.0)) // 1.0
// Results may not be exact, so we're only checking that they are within epsilon from the expected value
// cos(π/2) = 0
println("cos(PI / 2).absoluteValue < epsilon is ${cos(PI / 2).absoluteValue < epsilon}") // true
println(cos(PI)) // -1.0
// special cases
println(cos(Double.NaN)) // NaN
println(cos(Double.POSITIVE_INFINITY)) // NaN
println(cos(Double.NEGATIVE_INFINITY)) // NaN
//sampleEnd
}
actual external fun cos(x: Float): Float
Вычисляет косинус угла x, заданного в радианах.
Особые случаи:
cos(NaN|+Inf|-Inf)— этоNaN
Примеры
import kotlin.math.*
import kotlin.test.*
fun main() {
//sampleStart
val epsilon = 1e-6f
println(cos(0.0f)) // 1.0
// Results may not be exact, so we're only checking that they are within epsilon from the expected value
// cos(π/2) = 0
println("cos(PI.toFloat() / 2).absoluteValue < epsilon is ${cos(PI.toFloat() / 2).absoluteValue < epsilon}") // true
println(cos(PI.toFloat())) // -1.0
// special cases
println(cos(Float.NaN)) // NaN
println(cos(Float.POSITIVE_INFINITY)) // NaN
println(cos(Float.NEGATIVE_INFINITY)) // NaN
//sampleEnd
}
© 2010–2026 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.math/cos.html