atan2

expect fun atan2(y: Double, x: Double): Double(source)

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) is 0.0

  • atan2(0.0, x) is 0.0 for x > 0 and PI for x < 0

  • atan2(-0.0, x) is -0.0 for x > 0 and -PI for x < 0

  • atan2(y, +Inf) is 0.0 for 0 < y < +Inf and -0.0 for -Inf < y < 0

  • atan2(y, -Inf) is PI for 0 < y < +Inf and -PI for -Inf < y < 0

  • atan2(y, 0.0) is PI/2 for y > 0 and -PI/2 for y < 0

  • atan2(+Inf, x) is PI/2 for finite x

  • atan2(-Inf, x) is -PI/2 for finite x

  • atan2(NaN, x) and atan2(y, NaN) is NaN

Since Kotlin

1.2

See 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
}

expect fun atan2(y: Float, x: Float): Float(source)

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) is 0.0

  • atan2(0.0, x) is 0.0 for x > 0 and PI for x < 0

  • atan2(-0.0, x) is -0.0 for x > 0 and -PI for x < 0

  • atan2(y, +Inf) is 0.0 for 0 < y < +Inf and -0.0 for -Inf < y < 0

  • atan2(y, -Inf) is PI for 0 < y < +Inf and -PI for -Inf < y < 0

  • atan2(y, 0.0) is PI/2 for y > 0 and -PI/2 for y < 0

  • atan2(+Inf, x) is PI/2 for finite x

  • atan2(-Inf, x) is -PI/2 for finite x

  • atan2(NaN, x) and atan2(y, NaN) is NaN

Since Kotlin

1.2

See 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
}
actual inline fun atan2(y: Double, x: Double): Double(source)

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) is 0.0

  • atan2(0.0, x) is 0.0 for x > 0 and PI for x < 0

  • atan2(-0.0, x) is -0.0 for x > 0 and -PI for x < 0

  • atan2(y, +Inf) is 0.0 for 0 < y < +Inf and -0.0 for -Inf < y < 0

  • atan2(y, -Inf) is PI for 0 < y < +Inf and -PI for -Inf < y < 0

  • atan2(y, 0.0) is PI/2 for y > 0 and -PI/2 for y < 0

  • atan2(+Inf, x) is PI/2 for finite x

  • atan2(-Inf, x) is -PI/2 for finite x

  • atan2(NaN, x) and atan2(y, NaN) is NaN

Since Kotlin

1.2

See 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
}

actual inline fun atan2(y: Float, x: Float): Float(source)

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) is 0.0

  • atan2(0.0, x) is 0.0 for x > 0 and PI for x < 0

  • atan2(-0.0, x) is -0.0 for x > 0 and -PI for x < 0

  • atan2(y, +Inf) is 0.0 for 0 < y < +Inf and -0.0 for -Inf < y < 0

  • atan2(y, -Inf) is PI for 0 < y < +Inf and -PI for -Inf < y < 0

  • atan2(y, 0.0) is PI/2 for y > 0 and -PI/2 for y < 0

  • atan2(+Inf, x) is PI/2 for finite x

  • atan2(-Inf, x) is -PI/2 for finite x

  • atan2(NaN, x) and atan2(y, NaN) is NaN

Since Kotlin

1.2

See 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
}
actual inline fun atan2(y: Double, x: Double): Double(source)

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) is 0.0

  • atan2(0.0, x) is 0.0 for x > 0 and PI for x < 0

  • atan2(-0.0, x) is -0.0 for x > 0 and -PI for x < 0

  • atan2(y, +Inf) is 0.0 for 0 < y < +Inf and -0.0 for -Inf < y < 0

  • atan2(y, -Inf) is PI for 0 < y < +Inf and -PI for -Inf < y < 0

  • atan2(y, 0.0) is PI/2 for y > 0 and -PI/2 for y < 0

  • atan2(+Inf, x) is PI/2 for finite x

  • atan2(-Inf, x) is -PI/2 for finite x

  • atan2(NaN, x) and atan2(y, NaN) is NaN

Since Kotlin

1.2

See 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
}

actual inline fun atan2(y: Float, x: Float): Float(source)

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) is 0.0

  • atan2(0.0, x) is 0.0 for x > 0 and PI for x < 0

  • atan2(-0.0, x) is -0.0 for x > 0 and -PI for x < 0

  • atan2(y, +Inf) is 0.0 for 0 < y < +Inf and -0.0 for -Inf < y < 0

  • atan2(y, -Inf) is PI for 0 < y < +Inf and -PI for -Inf < y < 0

  • atan2(y, 0.0) is PI/2 for y > 0 and -PI/2 for y < 0

  • atan2(+Inf, x) is PI/2 for finite x

  • atan2(-Inf, x) is -PI/2 for finite x

  • atan2(NaN, x) and atan2(y, NaN) is NaN

Since Kotlin

1.2

See 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
}
actual external fun atan2(y: Double, x: Double): Double(source)

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) is 0.0

  • atan2(0.0, x) is 0.0 for x > 0 and PI for x < 0

  • atan2(-0.0, x) is -0.0 for x > 0 and -PI for x < 0

  • atan2(y, +Inf) is 0.0 for 0 < y < +Inf and -0.0 for -Inf < y < 0

  • atan2(y, -Inf) is PI for 0 < y < +Inf and -PI for -Inf < y < 0

  • atan2(y, 0.0) is PI/2 for y > 0 and -PI/2 for y < 0

  • atan2(+Inf, x) is PI/2 for finite x

  • atan2(-Inf, x) is -PI/2 for finite x

  • atan2(NaN, x) and atan2(y, NaN) is NaN

Since Kotlin

1.3

See 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
}

actual external fun atan2(y: Float, x: Float): Float(source)

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) is 0.0

  • atan2(0.0, x) is 0.0 for x > 0 and PI for x < 0

  • atan2(-0.0, x) is -0.0 for x > 0 and -PI for x < 0

  • atan2(y, +Inf) is 0.0 for 0 < y < +Inf and -0.0 for -Inf < y < 0

  • atan2(y, -Inf) is PI for 0 < y < +Inf and -PI for -Inf < y < 0

  • atan2(y, 0.0) is PI/2 for y > 0 and -PI/2 for y < 0

  • atan2(+Inf, x) is PI/2 for finite x

  • atan2(-Inf, x) is -PI/2 for finite x

  • atan2(NaN, x) and atan2(y, NaN) is NaN

Since Kotlin

1.3

See 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
}
actual fun atan2(y: Double, x: Double): Double(source)

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) is 0.0

  • atan2(0.0, x) is 0.0 for x > 0 and PI for x < 0

  • atan2(-0.0, x) is -0.0 for x > 0 and -PI for x < 0

  • atan2(y, +Inf) is 0.0 for 0 < y < +Inf and -0.0 for -Inf < y < 0

  • atan2(y, -Inf) is PI for 0 < y < +Inf and -PI for -Inf < y < 0

  • atan2(y, 0.0) is PI/2 for y > 0 and -PI/2 for y < 0

  • atan2(+Inf, x) is PI/2 for finite x

  • atan2(-Inf, x) is -PI/2 for finite x

  • atan2(NaN, x) and atan2(y, NaN) is NaN

Since Kotlin

1.8

See 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
}

actual fun atan2(y: Float, x: Float): Float(source)

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) is 0.0

  • atan2(0.0, x) is 0.0 for x > 0 and PI for x < 0

  • atan2(-0.0, x) is -0.0 for x > 0 and -PI for x < 0

  • atan2(y, +Inf) is 0.0 for 0 < y < +Inf and -0.0 for -Inf < y < 0

  • atan2(y, -Inf) is PI for 0 < y < +Inf and -PI for -Inf < y < 0

  • atan2(y, 0.0) is PI/2 for y > 0 and -PI/2 for y < 0

  • atan2(+Inf, x) is PI/2 for finite x

  • atan2(-Inf, x) is -PI/2 for finite x

  • atan2(NaN, x) and atan2(y, NaN) is NaN

Since Kotlin

1.8

See 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
}
actual fun atan2(y: Double, x: Double): Double(source)

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) is 0.0

  • atan2(0.0, x) is 0.0 for x > 0 and PI for x < 0

  • atan2(-0.0, x) is -0.0 for x > 0 and -PI for x < 0

  • atan2(y, +Inf) is 0.0 for 0 < y < +Inf and -0.0 for -Inf < y < 0

  • atan2(y, -Inf) is PI for 0 < y < +Inf and -PI for -Inf < y < 0

  • atan2(y, 0.0) is PI/2 for y > 0 and -PI/2 for y < 0

  • atan2(+Inf, x) is PI/2 for finite x

  • atan2(-Inf, x) is -PI/2 for finite x

  • atan2(NaN, x) and atan2(y, NaN) is NaN

Since Kotlin

1.8

See 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
}

actual fun atan2(y: Float, x: Float): Float(source)

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) is 0.0

  • atan2(0.0, x) is 0.0 for x > 0 and PI for x < 0

  • atan2(-0.0, x) is -0.0 for x > 0 and -PI for x < 0

  • atan2(y, +Inf) is 0.0 for 0 < y < +Inf and -0.0 for -Inf < y < 0

  • atan2(y, -Inf) is PI for 0 < y < +Inf and -PI for -Inf < y < 0

  • atan2(y, 0.0) is PI/2 for y > 0 and -PI/2 for y < 0

  • atan2(+Inf, x) is PI/2 for finite x

  • atan2(-Inf, x) is -PI/2 for finite x

  • atan2(NaN, x) and atan2(y, NaN) is NaN

Since Kotlin

1.8

See 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
}