atan2
Returns the angle theta of the polar coordinates (r, theta) that correspond to the rectangular coordinates (x, y) by computing the arc tangent of the value y / x; the returned value is an angle in the range from -PI to PI radians.
In other words, this function returns an angle in radians between the positive x-axis and a ray from the origin ((0, 0)) to the point (x, y) confined to the interval (-π, π].
The r component (the distance) of the polar coordinates (r, theta) could be calculated as hypot(x, y).
Special cases:
atan2(0.0, 0.0)is0.0atan2(0.0, x)is0.0forx > 0andPIforx < 0atan2(-0.0, x)is-0.0forx > 0and-PIforx < 0atan2(y, +Inf)is0.0for0 < y < +Infand-0.0for-Inf < y < 0atan2(y, -Inf)isPIfor0 < y < +Infand-PIfor-Inf < y < 0atan2(y, 0.0)isPI/2fory > 0and-PI/2fory < 0atan2(+Inf, x)isPI/2for finitexatan2(-Inf, x)is-PI/2for finitexatan2(NaN, x)andatan2(y, NaN)isNaN
Since Kotlin
1.2See also
function.
Samples
import kotlin.math.*
import kotlin.test.*
fun main() {
//sampleStart
val epsilon = 1e-10
fun Double.toDegrees(): Double = this * 180.0 / PI
println(atan2(y = 0.0, x = 0.0)) // 0.0
// Results may not be exact, so we're only checking that they are within epsilon from the expected value
println("(atan2(y = 1.0, x = 0.0) - PI / 2).absoluteValue < epsilon is ${(atan2(y = 1.0, x = 0.0) - PI / 2).absoluteValue < epsilon}") // true
println(atan2(y = 1.0, x = 0.0).toDegrees()) // 90.0
println("(atan2(y = 0.0, x = -1.0) - PI).absoluteValue < epsilon is ${(atan2(y = 0.0, x = -1.0) - PI).absoluteValue < epsilon}") // true
println(atan2(y = 0.0, x = -1.0).toDegrees()) // 180.0
println("(atan2(y = -1.0, x = 0.0) - (-PI / 2)).absoluteValue < epsilon is ${(atan2(y = -1.0, x = 0.0) - (-PI / 2)).absoluteValue < epsilon}") // true
println(atan2(y = -1.0, x = 0.0).toDegrees()) // -90.0
// special cases (some of them)
println(atan2(y = Double.NaN, x = 0.5)) // NaN
println(atan2(y = 0.5, x = Double.NaN)) // NaN
println(atan2(y = -0.0, x = 100500.0)) // -0.0
println(atan2(y = 0.0, x = Double.POSITIVE_INFINITY)) // 0.0
println(atan2(y = Double.POSITIVE_INFINITY, x = 100500.0).toDegrees()) // 90.0
//sampleEnd
}Returns the angle theta of the polar coordinates (r, theta) that correspond to the rectangular coordinates (x, y) by computing the arc tangent of the value y / x; the returned value is an angle in the range from -PI to PI radians.
In other words, this function returns an angle in radians between the positive x-axis and a ray from the origin ((0, 0)) to the point (x, y) confined to the interval (-π, π].
The r component (the distance) of the polar coordinates (r, theta) could be calculated as hypot(x, y).
Special cases:
atan2(0.0, 0.0)is0.0atan2(0.0, x)is0.0forx > 0andPIforx < 0atan2(-0.0, x)is-0.0forx > 0and-PIforx < 0atan2(y, +Inf)is0.0for0 < y < +Infand-0.0for-Inf < y < 0atan2(y, -Inf)isPIfor0 < y < +Infand-PIfor-Inf < y < 0atan2(y, 0.0)isPI/2fory > 0and-PI/2fory < 0atan2(+Inf, x)isPI/2for finitexatan2(-Inf, x)is-PI/2for finitexatan2(NaN, x)andatan2(y, NaN)isNaN
Since Kotlin
1.2See also
function.
Samples
import kotlin.math.*
import kotlin.test.*
fun main() {
//sampleStart
val epsilon = 1e-6f
fun Float.toDegrees(): Float = this * 180.0f / PI.toFloat()
println(atan2(y = 0.0f, x = 0.0f)) // 0.0
// Results may not be exact, so we're only checking that they are within epsilon from the expected value
println("(atan2(y = 1.0f, x = 0.0f) - PI.toFloat() / 2).absoluteValue < epsilon is ${(atan2(y = 1.0f, x = 0.0f) - PI.toFloat() / 2).absoluteValue < epsilon}") // true
println(atan2(y = 1.0f, x = 0.0f).toDegrees()) // 90.0
println("(atan2(y = 0.0f, x = -1.0f) - PI.toFloat()).absoluteValue < epsilon is ${(atan2(y = 0.0f, x = -1.0f) - PI.toFloat()).absoluteValue < epsilon}") // true
println(atan2(y = 0.0f, x = -1.0f).toDegrees()) // 180.0
println("(atan2(y = -1.0f, x = 0.0f) - (-PI.toFloat() / 2)).absoluteValue < epsilon is ${(atan2(y = -1.0f, x = 0.0f) - (-PI.toFloat() / 2)).absoluteValue < epsilon}") // true
println(atan2(y = -1.0f, x = 0.0f).toDegrees()) // -90.0
// special cases (some of them)
println(atan2(y = Float.NaN, x = 0.5f)) // NaN
println(atan2(y = 0.5f, x = Float.NaN)) // NaN
println(atan2(y = -0.0f, x = 100500.0f)) // -0.0
println(atan2(y = 0.0f, x = Float.POSITIVE_INFINITY)) // 0.0
println(atan2(y = Float.POSITIVE_INFINITY, x = 100500.0f).toDegrees()) // 90.0
//sampleEnd
}Returns the angle theta of the polar coordinates (r, theta) that correspond to the rectangular coordinates (x, y) by computing the arc tangent of the value y / x; the returned value is an angle in the range from -PI to PI radians.
In other words, this function returns an angle in radians between the positive x-axis and a ray from the origin ((0, 0)) to the point (x, y) confined to the interval (-π, π].
The r component (the distance) of the polar coordinates (r, theta) could be calculated as hypot(x, y).
Special cases:
atan2(0.0, 0.0)is0.0atan2(0.0, x)is0.0forx > 0andPIforx < 0atan2(-0.0, x)is-0.0forx > 0and-PIforx < 0atan2(y, +Inf)is0.0for0 < y < +Infand-0.0for-Inf < y < 0atan2(y, -Inf)isPIfor0 < y < +Infand-PIfor-Inf < y < 0atan2(y, 0.0)isPI/2fory > 0and-PI/2fory < 0atan2(+Inf, x)isPI/2for finitexatan2(-Inf, x)is-PI/2for finitexatan2(NaN, x)andatan2(y, NaN)isNaN
Since Kotlin
1.2See also
function.
Samples
import kotlin.math.*
import kotlin.test.*
fun main() {
//sampleStart
val epsilon = 1e-10
fun Double.toDegrees(): Double = this * 180.0 / PI
println(atan2(y = 0.0, x = 0.0)) // 0.0
// Results may not be exact, so we're only checking that they are within epsilon from the expected value
println("(atan2(y = 1.0, x = 0.0) - PI / 2).absoluteValue < epsilon is ${(atan2(y = 1.0, x = 0.0) - PI / 2).absoluteValue < epsilon}") // true
println(atan2(y = 1.0, x = 0.0).toDegrees()) // 90.0
println("(atan2(y = 0.0, x = -1.0) - PI).absoluteValue < epsilon is ${(atan2(y = 0.0, x = -1.0) - PI).absoluteValue < epsilon}") // true
println(atan2(y = 0.0, x = -1.0).toDegrees()) // 180.0
println("(atan2(y = -1.0, x = 0.0) - (-PI / 2)).absoluteValue < epsilon is ${(atan2(y = -1.0, x = 0.0) - (-PI / 2)).absoluteValue < epsilon}") // true
println(atan2(y = -1.0, x = 0.0).toDegrees()) // -90.0
// special cases (some of them)
println(atan2(y = Double.NaN, x = 0.5)) // NaN
println(atan2(y = 0.5, x = Double.NaN)) // NaN
println(atan2(y = -0.0, x = 100500.0)) // -0.0
println(atan2(y = 0.0, x = Double.POSITIVE_INFINITY)) // 0.0
println(atan2(y = Double.POSITIVE_INFINITY, x = 100500.0).toDegrees()) // 90.0
//sampleEnd
}Returns the angle theta of the polar coordinates (r, theta) that correspond to the rectangular coordinates (x, y) by computing the arc tangent of the value y / x; the returned value is an angle in the range from -PI to PI radians.
In other words, this function returns an angle in radians between the positive x-axis and a ray from the origin ((0, 0)) to the point (x, y) confined to the interval (-π, π].
The r component (the distance) of the polar coordinates (r, theta) could be calculated as hypot(x, y).
Special cases:
atan2(0.0, 0.0)is0.0atan2(0.0, x)is0.0forx > 0andPIforx < 0atan2(-0.0, x)is-0.0forx > 0and-PIforx < 0atan2(y, +Inf)is0.0for0 < y < +Infand-0.0for-Inf < y < 0atan2(y, -Inf)isPIfor0 < y < +Infand-PIfor-Inf < y < 0atan2(y, 0.0)isPI/2fory > 0and-PI/2fory < 0atan2(+Inf, x)isPI/2for finitexatan2(-Inf, x)is-PI/2for finitexatan2(NaN, x)andatan2(y, NaN)isNaN
Since Kotlin
1.2See also
function.
Samples
import kotlin.math.*
import kotlin.test.*
fun main() {
//sampleStart
val epsilon = 1e-6f
fun Float.toDegrees(): Float = this * 180.0f / PI.toFloat()
println(atan2(y = 0.0f, x = 0.0f)) // 0.0
// Results may not be exact, so we're only checking that they are within epsilon from the expected value
println("(atan2(y = 1.0f, x = 0.0f) - PI.toFloat() / 2).absoluteValue < epsilon is ${(atan2(y = 1.0f, x = 0.0f) - PI.toFloat() / 2).absoluteValue < epsilon}") // true
println(atan2(y = 1.0f, x = 0.0f).toDegrees()) // 90.0
println("(atan2(y = 0.0f, x = -1.0f) - PI.toFloat()).absoluteValue < epsilon is ${(atan2(y = 0.0f, x = -1.0f) - PI.toFloat()).absoluteValue < epsilon}") // true
println(atan2(y = 0.0f, x = -1.0f).toDegrees()) // 180.0
println("(atan2(y = -1.0f, x = 0.0f) - (-PI.toFloat() / 2)).absoluteValue < epsilon is ${(atan2(y = -1.0f, x = 0.0f) - (-PI.toFloat() / 2)).absoluteValue < epsilon}") // true
println(atan2(y = -1.0f, x = 0.0f).toDegrees()) // -90.0
// special cases (some of them)
println(atan2(y = Float.NaN, x = 0.5f)) // NaN
println(atan2(y = 0.5f, x = Float.NaN)) // NaN
println(atan2(y = -0.0f, x = 100500.0f)) // -0.0
println(atan2(y = 0.0f, x = Float.POSITIVE_INFINITY)) // 0.0
println(atan2(y = Float.POSITIVE_INFINITY, x = 100500.0f).toDegrees()) // 90.0
//sampleEnd
}Returns the angle theta of the polar coordinates (r, theta) that correspond to the rectangular coordinates (x, y) by computing the arc tangent of the value y / x; the returned value is an angle in the range from -PI to PI radians.
In other words, this function returns an angle in radians between the positive x-axis and a ray from the origin ((0, 0)) to the point (x, y) confined to the interval (-π, π].
The r component (the distance) of the polar coordinates (r, theta) could be calculated as hypot(x, y).
Special cases:
atan2(0.0, 0.0)is0.0atan2(0.0, x)is0.0forx > 0andPIforx < 0atan2(-0.0, x)is-0.0forx > 0and-PIforx < 0atan2(y, +Inf)is0.0for0 < y < +Infand-0.0for-Inf < y < 0atan2(y, -Inf)isPIfor0 < y < +Infand-PIfor-Inf < y < 0atan2(y, 0.0)isPI/2fory > 0and-PI/2fory < 0atan2(+Inf, x)isPI/2for finitexatan2(-Inf, x)is-PI/2for finitexatan2(NaN, x)andatan2(y, NaN)isNaN
Since Kotlin
1.2See also
function.
Samples
import kotlin.math.*
import kotlin.test.*
fun main() {
//sampleStart
val epsilon = 1e-10
fun Double.toDegrees(): Double = this * 180.0 / PI
println(atan2(y = 0.0, x = 0.0)) // 0.0
// Results may not be exact, so we're only checking that they are within epsilon from the expected value
println("(atan2(y = 1.0, x = 0.0) - PI / 2).absoluteValue < epsilon is ${(atan2(y = 1.0, x = 0.0) - PI / 2).absoluteValue < epsilon}") // true
println(atan2(y = 1.0, x = 0.0).toDegrees()) // 90.0
println("(atan2(y = 0.0, x = -1.0) - PI).absoluteValue < epsilon is ${(atan2(y = 0.0, x = -1.0) - PI).absoluteValue < epsilon}") // true
println(atan2(y = 0.0, x = -1.0).toDegrees()) // 180.0
println("(atan2(y = -1.0, x = 0.0) - (-PI / 2)).absoluteValue < epsilon is ${(atan2(y = -1.0, x = 0.0) - (-PI / 2)).absoluteValue < epsilon}") // true
println(atan2(y = -1.0, x = 0.0).toDegrees()) // -90.0
// special cases (some of them)
println(atan2(y = Double.NaN, x = 0.5)) // NaN
println(atan2(y = 0.5, x = Double.NaN)) // NaN
println(atan2(y = -0.0, x = 100500.0)) // -0.0
println(atan2(y = 0.0, x = Double.POSITIVE_INFINITY)) // 0.0
println(atan2(y = Double.POSITIVE_INFINITY, x = 100500.0).toDegrees()) // 90.0
//sampleEnd
}Returns the angle theta of the polar coordinates (r, theta) that correspond to the rectangular coordinates (x, y) by computing the arc tangent of the value y / x; the returned value is an angle in the range from -PI to PI radians.
In other words, this function returns an angle in radians between the positive x-axis and a ray from the origin ((0, 0)) to the point (x, y) confined to the interval (-π, π].
The r component (the distance) of the polar coordinates (r, theta) could be calculated as hypot(x, y).
Special cases:
atan2(0.0, 0.0)is0.0atan2(0.0, x)is0.0forx > 0andPIforx < 0atan2(-0.0, x)is-0.0forx > 0and-PIforx < 0atan2(y, +Inf)is0.0for0 < y < +Infand-0.0for-Inf < y < 0atan2(y, -Inf)isPIfor0 < y < +Infand-PIfor-Inf < y < 0atan2(y, 0.0)isPI/2fory > 0and-PI/2fory < 0atan2(+Inf, x)isPI/2for finitexatan2(-Inf, x)is-PI/2for finitexatan2(NaN, x)andatan2(y, NaN)isNaN
Since Kotlin
1.2See also
function.
Samples
import kotlin.math.*
import kotlin.test.*
fun main() {
//sampleStart
val epsilon = 1e-6f
fun Float.toDegrees(): Float = this * 180.0f / PI.toFloat()
println(atan2(y = 0.0f, x = 0.0f)) // 0.0
// Results may not be exact, so we're only checking that they are within epsilon from the expected value
println("(atan2(y = 1.0f, x = 0.0f) - PI.toFloat() / 2).absoluteValue < epsilon is ${(atan2(y = 1.0f, x = 0.0f) - PI.toFloat() / 2).absoluteValue < epsilon}") // true
println(atan2(y = 1.0f, x = 0.0f).toDegrees()) // 90.0
println("(atan2(y = 0.0f, x = -1.0f) - PI.toFloat()).absoluteValue < epsilon is ${(atan2(y = 0.0f, x = -1.0f) - PI.toFloat()).absoluteValue < epsilon}") // true
println(atan2(y = 0.0f, x = -1.0f).toDegrees()) // 180.0
println("(atan2(y = -1.0f, x = 0.0f) - (-PI.toFloat() / 2)).absoluteValue < epsilon is ${(atan2(y = -1.0f, x = 0.0f) - (-PI.toFloat() / 2)).absoluteValue < epsilon}") // true
println(atan2(y = -1.0f, x = 0.0f).toDegrees()) // -90.0
// special cases (some of them)
println(atan2(y = Float.NaN, x = 0.5f)) // NaN
println(atan2(y = 0.5f, x = Float.NaN)) // NaN
println(atan2(y = -0.0f, x = 100500.0f)) // -0.0
println(atan2(y = 0.0f, x = Float.POSITIVE_INFINITY)) // 0.0
println(atan2(y = Float.POSITIVE_INFINITY, x = 100500.0f).toDegrees()) // 90.0
//sampleEnd
}Returns the angle theta of the polar coordinates (r, theta) that correspond to the rectangular coordinates (x, y) by computing the arc tangent of the value y / x; the returned value is an angle in the range from -PI to PI radians.
In other words, this function returns an angle in radians between the positive x-axis and a ray from the origin ((0, 0)) to the point (x, y) confined to the interval (-π, π].
The r component (the distance) of the polar coordinates (r, theta) could be calculated as hypot(x, y).
Special cases:
atan2(0.0, 0.0)is0.0atan2(0.0, x)is0.0forx > 0andPIforx < 0atan2(-0.0, x)is-0.0forx > 0and-PIforx < 0atan2(y, +Inf)is0.0for0 < y < +Infand-0.0for-Inf < y < 0atan2(y, -Inf)isPIfor0 < y < +Infand-PIfor-Inf < y < 0atan2(y, 0.0)isPI/2fory > 0and-PI/2fory < 0atan2(+Inf, x)isPI/2for finitexatan2(-Inf, x)is-PI/2for finitexatan2(NaN, x)andatan2(y, NaN)isNaN
Since Kotlin
1.3See also
function.
Samples
import kotlin.math.*
import kotlin.test.*
fun main() {
//sampleStart
val epsilon = 1e-10
fun Double.toDegrees(): Double = this * 180.0 / PI
println(atan2(y = 0.0, x = 0.0)) // 0.0
// Results may not be exact, so we're only checking that they are within epsilon from the expected value
println("(atan2(y = 1.0, x = 0.0) - PI / 2).absoluteValue < epsilon is ${(atan2(y = 1.0, x = 0.0) - PI / 2).absoluteValue < epsilon}") // true
println(atan2(y = 1.0, x = 0.0).toDegrees()) // 90.0
println("(atan2(y = 0.0, x = -1.0) - PI).absoluteValue < epsilon is ${(atan2(y = 0.0, x = -1.0) - PI).absoluteValue < epsilon}") // true
println(atan2(y = 0.0, x = -1.0).toDegrees()) // 180.0
println("(atan2(y = -1.0, x = 0.0) - (-PI / 2)).absoluteValue < epsilon is ${(atan2(y = -1.0, x = 0.0) - (-PI / 2)).absoluteValue < epsilon}") // true
println(atan2(y = -1.0, x = 0.0).toDegrees()) // -90.0
// special cases (some of them)
println(atan2(y = Double.NaN, x = 0.5)) // NaN
println(atan2(y = 0.5, x = Double.NaN)) // NaN
println(atan2(y = -0.0, x = 100500.0)) // -0.0
println(atan2(y = 0.0, x = Double.POSITIVE_INFINITY)) // 0.0
println(atan2(y = Double.POSITIVE_INFINITY, x = 100500.0).toDegrees()) // 90.0
//sampleEnd
}Returns the angle theta of the polar coordinates (r, theta) that correspond to the rectangular coordinates (x, y) by computing the arc tangent of the value y / x; the returned value is an angle in the range from -PI to PI radians.
In other words, this function returns an angle in radians between the positive x-axis and a ray from the origin ((0, 0)) to the point (x, y) confined to the interval (-π, π].
The r component (the distance) of the polar coordinates (r, theta) could be calculated as hypot(x, y).
Special cases:
atan2(0.0, 0.0)is0.0atan2(0.0, x)is0.0forx > 0andPIforx < 0atan2(-0.0, x)is-0.0forx > 0and-PIforx < 0atan2(y, +Inf)is0.0for0 < y < +Infand-0.0for-Inf < y < 0atan2(y, -Inf)isPIfor0 < y < +Infand-PIfor-Inf < y < 0atan2(y, 0.0)isPI/2fory > 0and-PI/2fory < 0atan2(+Inf, x)isPI/2for finitexatan2(-Inf, x)is-PI/2for finitexatan2(NaN, x)andatan2(y, NaN)isNaN
Since Kotlin
1.3See also
function.
Samples
import kotlin.math.*
import kotlin.test.*
fun main() {
//sampleStart
val epsilon = 1e-6f
fun Float.toDegrees(): Float = this * 180.0f / PI.toFloat()
println(atan2(y = 0.0f, x = 0.0f)) // 0.0
// Results may not be exact, so we're only checking that they are within epsilon from the expected value
println("(atan2(y = 1.0f, x = 0.0f) - PI.toFloat() / 2).absoluteValue < epsilon is ${(atan2(y = 1.0f, x = 0.0f) - PI.toFloat() / 2).absoluteValue < epsilon}") // true
println(atan2(y = 1.0f, x = 0.0f).toDegrees()) // 90.0
println("(atan2(y = 0.0f, x = -1.0f) - PI.toFloat()).absoluteValue < epsilon is ${(atan2(y = 0.0f, x = -1.0f) - PI.toFloat()).absoluteValue < epsilon}") // true
println(atan2(y = 0.0f, x = -1.0f).toDegrees()) // 180.0
println("(atan2(y = -1.0f, x = 0.0f) - (-PI.toFloat() / 2)).absoluteValue < epsilon is ${(atan2(y = -1.0f, x = 0.0f) - (-PI.toFloat() / 2)).absoluteValue < epsilon}") // true
println(atan2(y = -1.0f, x = 0.0f).toDegrees()) // -90.0
// special cases (some of them)
println(atan2(y = Float.NaN, x = 0.5f)) // NaN
println(atan2(y = 0.5f, x = Float.NaN)) // NaN
println(atan2(y = -0.0f, x = 100500.0f)) // -0.0
println(atan2(y = 0.0f, x = Float.POSITIVE_INFINITY)) // 0.0
println(atan2(y = Float.POSITIVE_INFINITY, x = 100500.0f).toDegrees()) // 90.0
//sampleEnd
}Returns the angle theta of the polar coordinates (r, theta) that correspond to the rectangular coordinates (x, y) by computing the arc tangent of the value y / x; the returned value is an angle in the range from -PI to PI radians.
In other words, this function returns an angle in radians between the positive x-axis and a ray from the origin ((0, 0)) to the point (x, y) confined to the interval (-π, π].
The r component (the distance) of the polar coordinates (r, theta) could be calculated as hypot(x, y).
Special cases:
atan2(0.0, 0.0)is0.0atan2(0.0, x)is0.0forx > 0andPIforx < 0atan2(-0.0, x)is-0.0forx > 0and-PIforx < 0atan2(y, +Inf)is0.0for0 < y < +Infand-0.0for-Inf < y < 0atan2(y, -Inf)isPIfor0 < y < +Infand-PIfor-Inf < y < 0atan2(y, 0.0)isPI/2fory > 0and-PI/2fory < 0atan2(+Inf, x)isPI/2for finitexatan2(-Inf, x)is-PI/2for finitexatan2(NaN, x)andatan2(y, NaN)isNaN
Since Kotlin
1.8See also
function.
Samples
import kotlin.math.*
import kotlin.test.*
fun main() {
//sampleStart
val epsilon = 1e-10
fun Double.toDegrees(): Double = this * 180.0 / PI
println(atan2(y = 0.0, x = 0.0)) // 0.0
// Results may not be exact, so we're only checking that they are within epsilon from the expected value
println("(atan2(y = 1.0, x = 0.0) - PI / 2).absoluteValue < epsilon is ${(atan2(y = 1.0, x = 0.0) - PI / 2).absoluteValue < epsilon}") // true
println(atan2(y = 1.0, x = 0.0).toDegrees()) // 90.0
println("(atan2(y = 0.0, x = -1.0) - PI).absoluteValue < epsilon is ${(atan2(y = 0.0, x = -1.0) - PI).absoluteValue < epsilon}") // true
println(atan2(y = 0.0, x = -1.0).toDegrees()) // 180.0
println("(atan2(y = -1.0, x = 0.0) - (-PI / 2)).absoluteValue < epsilon is ${(atan2(y = -1.0, x = 0.0) - (-PI / 2)).absoluteValue < epsilon}") // true
println(atan2(y = -1.0, x = 0.0).toDegrees()) // -90.0
// special cases (some of them)
println(atan2(y = Double.NaN, x = 0.5)) // NaN
println(atan2(y = 0.5, x = Double.NaN)) // NaN
println(atan2(y = -0.0, x = 100500.0)) // -0.0
println(atan2(y = 0.0, x = Double.POSITIVE_INFINITY)) // 0.0
println(atan2(y = Double.POSITIVE_INFINITY, x = 100500.0).toDegrees()) // 90.0
//sampleEnd
}Returns the angle theta of the polar coordinates (r, theta) that correspond to the rectangular coordinates (x, y) by computing the arc tangent of the value y / x; the returned value is an angle in the range from -PI to PI radians.
In other words, this function returns an angle in radians between the positive x-axis and a ray from the origin ((0, 0)) to the point (x, y) confined to the interval (-π, π].
The r component (the distance) of the polar coordinates (r, theta) could be calculated as hypot(x, y).
Special cases:
atan2(0.0, 0.0)is0.0atan2(0.0, x)is0.0forx > 0andPIforx < 0atan2(-0.0, x)is-0.0forx > 0and-PIforx < 0atan2(y, +Inf)is0.0for0 < y < +Infand-0.0for-Inf < y < 0atan2(y, -Inf)isPIfor0 < y < +Infand-PIfor-Inf < y < 0atan2(y, 0.0)isPI/2fory > 0and-PI/2fory < 0atan2(+Inf, x)isPI/2for finitexatan2(-Inf, x)is-PI/2for finitexatan2(NaN, x)andatan2(y, NaN)isNaN
Since Kotlin
1.8See also
function.
Samples
import kotlin.math.*
import kotlin.test.*
fun main() {
//sampleStart
val epsilon = 1e-6f
fun Float.toDegrees(): Float = this * 180.0f / PI.toFloat()
println(atan2(y = 0.0f, x = 0.0f)) // 0.0
// Results may not be exact, so we're only checking that they are within epsilon from the expected value
println("(atan2(y = 1.0f, x = 0.0f) - PI.toFloat() / 2).absoluteValue < epsilon is ${(atan2(y = 1.0f, x = 0.0f) - PI.toFloat() / 2).absoluteValue < epsilon}") // true
println(atan2(y = 1.0f, x = 0.0f).toDegrees()) // 90.0
println("(atan2(y = 0.0f, x = -1.0f) - PI.toFloat()).absoluteValue < epsilon is ${(atan2(y = 0.0f, x = -1.0f) - PI.toFloat()).absoluteValue < epsilon}") // true
println(atan2(y = 0.0f, x = -1.0f).toDegrees()) // 180.0
println("(atan2(y = -1.0f, x = 0.0f) - (-PI.toFloat() / 2)).absoluteValue < epsilon is ${(atan2(y = -1.0f, x = 0.0f) - (-PI.toFloat() / 2)).absoluteValue < epsilon}") // true
println(atan2(y = -1.0f, x = 0.0f).toDegrees()) // -90.0
// special cases (some of them)
println(atan2(y = Float.NaN, x = 0.5f)) // NaN
println(atan2(y = 0.5f, x = Float.NaN)) // NaN
println(atan2(y = -0.0f, x = 100500.0f)) // -0.0
println(atan2(y = 0.0f, x = Float.POSITIVE_INFINITY)) // 0.0
println(atan2(y = Float.POSITIVE_INFINITY, x = 100500.0f).toDegrees()) // 90.0
//sampleEnd
}Returns the angle theta of the polar coordinates (r, theta) that correspond to the rectangular coordinates (x, y) by computing the arc tangent of the value y / x; the returned value is an angle in the range from -PI to PI radians.
In other words, this function returns an angle in radians between the positive x-axis and a ray from the origin ((0, 0)) to the point (x, y) confined to the interval (-π, π].
The r component (the distance) of the polar coordinates (r, theta) could be calculated as hypot(x, y).
Special cases:
atan2(0.0, 0.0)is0.0atan2(0.0, x)is0.0forx > 0andPIforx < 0atan2(-0.0, x)is-0.0forx > 0and-PIforx < 0atan2(y, +Inf)is0.0for0 < y < +Infand-0.0for-Inf < y < 0atan2(y, -Inf)isPIfor0 < y < +Infand-PIfor-Inf < y < 0atan2(y, 0.0)isPI/2fory > 0and-PI/2fory < 0atan2(+Inf, x)isPI/2for finitexatan2(-Inf, x)is-PI/2for finitexatan2(NaN, x)andatan2(y, NaN)isNaN
Since Kotlin
1.8See also
function.
Samples
import kotlin.math.*
import kotlin.test.*
fun main() {
//sampleStart
val epsilon = 1e-10
fun Double.toDegrees(): Double = this * 180.0 / PI
println(atan2(y = 0.0, x = 0.0)) // 0.0
// Results may not be exact, so we're only checking that they are within epsilon from the expected value
println("(atan2(y = 1.0, x = 0.0) - PI / 2).absoluteValue < epsilon is ${(atan2(y = 1.0, x = 0.0) - PI / 2).absoluteValue < epsilon}") // true
println(atan2(y = 1.0, x = 0.0).toDegrees()) // 90.0
println("(atan2(y = 0.0, x = -1.0) - PI).absoluteValue < epsilon is ${(atan2(y = 0.0, x = -1.0) - PI).absoluteValue < epsilon}") // true
println(atan2(y = 0.0, x = -1.0).toDegrees()) // 180.0
println("(atan2(y = -1.0, x = 0.0) - (-PI / 2)).absoluteValue < epsilon is ${(atan2(y = -1.0, x = 0.0) - (-PI / 2)).absoluteValue < epsilon}") // true
println(atan2(y = -1.0, x = 0.0).toDegrees()) // -90.0
// special cases (some of them)
println(atan2(y = Double.NaN, x = 0.5)) // NaN
println(atan2(y = 0.5, x = Double.NaN)) // NaN
println(atan2(y = -0.0, x = 100500.0)) // -0.0
println(atan2(y = 0.0, x = Double.POSITIVE_INFINITY)) // 0.0
println(atan2(y = Double.POSITIVE_INFINITY, x = 100500.0).toDegrees()) // 90.0
//sampleEnd
}Returns the angle theta of the polar coordinates (r, theta) that correspond to the rectangular coordinates (x, y) by computing the arc tangent of the value y / x; the returned value is an angle in the range from -PI to PI radians.
In other words, this function returns an angle in radians between the positive x-axis and a ray from the origin ((0, 0)) to the point (x, y) confined to the interval (-π, π].
The r component (the distance) of the polar coordinates (r, theta) could be calculated as hypot(x, y).
Special cases:
atan2(0.0, 0.0)is0.0atan2(0.0, x)is0.0forx > 0andPIforx < 0atan2(-0.0, x)is-0.0forx > 0and-PIforx < 0atan2(y, +Inf)is0.0for0 < y < +Infand-0.0for-Inf < y < 0atan2(y, -Inf)isPIfor0 < y < +Infand-PIfor-Inf < y < 0atan2(y, 0.0)isPI/2fory > 0and-PI/2fory < 0atan2(+Inf, x)isPI/2for finitexatan2(-Inf, x)is-PI/2for finitexatan2(NaN, x)andatan2(y, NaN)isNaN
Since Kotlin
1.8See also
function.
Samples
import kotlin.math.*
import kotlin.test.*
fun main() {
//sampleStart
val epsilon = 1e-6f
fun Float.toDegrees(): Float = this * 180.0f / PI.toFloat()
println(atan2(y = 0.0f, x = 0.0f)) // 0.0
// Results may not be exact, so we're only checking that they are within epsilon from the expected value
println("(atan2(y = 1.0f, x = 0.0f) - PI.toFloat() / 2).absoluteValue < epsilon is ${(atan2(y = 1.0f, x = 0.0f) - PI.toFloat() / 2).absoluteValue < epsilon}") // true
println(atan2(y = 1.0f, x = 0.0f).toDegrees()) // 90.0
println("(atan2(y = 0.0f, x = -1.0f) - PI.toFloat()).absoluteValue < epsilon is ${(atan2(y = 0.0f, x = -1.0f) - PI.toFloat()).absoluteValue < epsilon}") // true
println(atan2(y = 0.0f, x = -1.0f).toDegrees()) // 180.0
println("(atan2(y = -1.0f, x = 0.0f) - (-PI.toFloat() / 2)).absoluteValue < epsilon is ${(atan2(y = -1.0f, x = 0.0f) - (-PI.toFloat() / 2)).absoluteValue < epsilon}") // true
println(atan2(y = -1.0f, x = 0.0f).toDegrees()) // -90.0
// special cases (some of them)
println(atan2(y = Float.NaN, x = 0.5f)) // NaN
println(atan2(y = 0.5f, x = Float.NaN)) // NaN
println(atan2(y = -0.0f, x = 100500.0f)) // -0.0
println(atan2(y = 0.0f, x = Float.POSITIVE_INFINITY)) // 0.0
println(atan2(y = Float.POSITIVE_INFINITY, x = 100500.0f).toDegrees()) // 90.0
//sampleEnd
}