# atan, atan2, and atanh: Three Different Inverses, One Confusing Name

> atan and atan2 both undo tangent, but atan takes one ratio and throws away which quadrant it came from, while atan2 takes the two original numbers so it can get the quadrant back. atanh is a different inverse wearing the same four letters.

**Date:** 2026-10-09T00:00:00.000Z
**Tags:** Canvas, Graphics, Math

## What atan, atan2, and atanh actually are

Three functions that share a naming pattern, each inverting something
different:

* **`atan(ratio)`** inverts `tan`. Feed it one number, a slope or a
  `dy / dx` (dx is how far a point moved along x, dy is how far it moved along y, both measured relative to some reference point (a drag's start position, a shape's center). Neither one is a coordinate by itself, just a difference between two.)
  ratio, and it gives back an angle, always somewhere between -90° and
  90°.
* **`atan2(dy, dx)`** also gives back an angle, but from `dy` and `dx`
  as two separate numbers instead of one pre-divided ratio. Its answers
  cover the full circle, -180° to 180°, not just half of it.
* **`atanh(x)`** isn't about angles at all. It inverts
  `tanh` (tanh, the hyperbolic tangent: a function that squashes every real number into the open interval (-1, 1), the way a sigmoid curve flattens out toward a ceiling and a floor.),
  expanding a number already squeezed into `(-1, 1)` back out to any
  real number. The detour section near the end of this post covers it
  on its own.

`atan` and `atan2` share a root word and both invert a tangent;
`atanh` only shares the naming pattern with the other two.

## The intuition

"Arctangent (The inverse of tan, also written tan⁻¹: given a ratio, it returns the angle whose tangent equals that ratio. atan, atan2, and atanh below are three different, non-interchangeable functions built around that idea.)"
sounds like one function, so it's tempting to treat `atan`, `atan2`, and
`atanh` as interchangeable spellings of the same idea. They aren't, and
`atan` versus `atan2` especially is where that assumption breaks:

* `tan(angle)` only depends on the *ratio* `dy / dx`. Two opposite
  points, like `(1, 1)` and `(-1, -1)`, produce the exact same ratio,
  because `1 / 1` and `-1 / -1` are both `1`.
* `atan` has to return a single answer for that ratio. It picks 45°,
  which is correct for `(1, 1)` and wrong for `(-1, -1)`, which is
  actually at -135°.
* `atan2` exists specifically to undo this information loss. It reads
  `dy` and `dx` separately, so it can check each one's sign and resolve
  all four quadrants correctly.

The drag-to-angle example from
[screen coordinate systems](/screen-coordinate-system) already
leaned on `atan2` for exactly this reason. This post stays on that one
mechanism long enough to see why `atan` can't do the same job, no matter
how the input is massaged.

* [Screen Coordinate Systems Aren't Cartesian](/screen-coordinate-system) — The drag-to-angle case this post keeps coming back to: why atan2 is the inverse that survives a y-down screen's flipped rotation direction.

* [Polar to Cartesian: What cos and sin Are Actually Computing](/polar-to-cartesian-point) — The forward direction atan2 undoes: turning an angle and radius into an (x, y) point in the first place.

## Concrete example: the same ratio, two different directions

```kotlin
import kotlin.math.atan
import kotlin.math.atan2

val dy1 = 1.0; val dx1 = 1.0     // down-and-right (y grows downward on screen)
val dy2 = -1.0; val dx2 = -1.0   // up-and-left, the opposite direction

// Same ratio (1/1 == -1/-1), so atan returns the same angle for both,
// even though the two points face opposite ways.
Math.toDegrees(atan(dy1 / dx1))   // 45.0
Math.toDegrees(atan(dy2 / dx2))   // 45.0 (wrong, this point is at -135°)

// atan2 sees dy and dx separately instead of one pre-divided ratio, so
// it tells the two points apart correctly.
Math.toDegrees(atan2(dy1, dx1))   // 45.0
Math.toDegrees(atan2(dy2, dx2))   // -135.0
```

`atan` isn't buggy here. It's answering the only question it was asked,
"what angle has this tangent," and a single ratio genuinely maps to two
different angles 180° apart. `atan2` is a different question: "what
angle gets to this `(dx, dy)` point," and that one has a unique answer
for every point except the origin itself.

*\[Interactive demo: AtanAmbiguityDemo — view this post in a browser for the live version.]*

Drag the dot through all four quadrants. `atan2` tracks the true angle
the whole way around. `atan` snaps back to the same reading every time
the drag crosses to the mirrored point on the other side of center,
because that's the only input it ever sees.

> **The fundamental**
>
> `atan2(dy, dx)` is a **two-argument inverse**. Where `atan(ratio)` only
> gets to see information that survives a division, `atan2` gets the
> numerator and denominator before anything is divided, so it can recover
> the quadrant a one-argument ratio already threw away.
>
> It's not a variant of `atan` with better defaults. It's solving a
> strictly larger problem: "which direction is this point," not "what
> angle has this tangent."

## Elsewhere: same fundamental, different framework

* **JavaScript**: `Math.atan2(y, x)` takes the same two arguments in the
  same order, for the same reason, down to the `(0, 0)` edge case below.
* **GLSL**: `atan(y, x)`, the two-argument overload of the same function
  name used for the one-argument version, resolves the full -π to π
  range in a fragment shader the same way, commonly used to turn a
  fragment's offset from a center into an angle for procedural shapes.
* **Swift**: `atan2(y, x)`, imported straight from C's libm with no
  argument labels, is the same function under the same name, because
  the underlying math, and the reason a second argument is needed,
  doesn't change across languages.

## Edge cases

`kotlin.math.atan2` on Android delegates straight to the JVM's
`Math.atan2`, so it inherits a defined answer for every input,
including the one input where "the direction to this point" sounds
like it shouldn't have an answer at all: the origin.

```kotlin
import kotlin.math.atan
import kotlin.math.atan2

val dy = 0.0
val dx = -0.0   // negative zero: a distinct, valid Double value

dy / dx          // NaN (0/0 is indeterminate no matter the signs involved)
atan(dy / dx)    // NaN, so there's no angle to read at all

atan2(dy, dx)    // ~3.14159 (π)
```

`atan2` never divides `dy` by `dx`, so it isn't exposed to the `0/0`
indeterminate form at all. It reads the sign of each zero directly:
positive `dy`, negative `dx` reads as "pointing in the negative-`x`
direction," which is `π`. Flip `dx` back to positive zero and `atan2`
returns positive zero instead, a different answer for a `Double` value
(`-0.0`) that `==` treats as equal to `0.0`. `atan`, working from a
ratio that already collapsed to `NaN`, has no way to tell these cases
apart.

## Where else atan2 shows up

Drag-to-rotate knobs and facing-direction logic, like the
[drag-to-angle demo](/screen-coordinate-system#dragging) from the
companion post on screen coordinate systems, are two entries in a much
longer list. Any time you need *direction* out of a `(dx, dy)` pair
instead of distance, `atan2` is usually how.

**UI and touch interaction**:

* **Drag-to-rotate knobs and dials** (volume knobs, color wheels,
  circular sliders, clock-style pickers): angle from center to touch
  point drives rotation.
* **Radial/pie menus**: hit-testing which wedge a tap landed in, by
  computing the tap's angle relative to the menu's center.
* **Swipe-direction classification**: bucket a swipe's `atan2(dy, dx)`
  into up/down/left/right (or 8-way) ranges.
* **Elements that track the pointer**: an arrow or indicator that always
  points at the cursor, a radial selection highlight.

**Movement and orientation**:

* **Facing direction from a velocity vector**: rotate a sprite/object to
  face where it's moving, using `atan2(dy, dx)` of its per-frame
  displacement.
* **"Look at"/aim logic**: turret rotation, enemy-faces-player in games,
  camera-faces-target.
* **Joystick/gamepad input**: splitting an analog stick's raw `(x, y)`
  into direction (angle) and magnitude (radius) for 8-directional
  movement or deadzone handling.
* **Bearing between two points**: `atan2(y2 - y1, x2 - x1)` for
  navigation/pathfinding; the GPS/compass-heading version is the same
  idea on a sphere.

**Graphics and data viz**:

* **Arc and pie-chart geometry**: computing a segment's start/end angle,
  or, in reverse, which slice a click landed in.
* **Cartesian-to-polar conversion generally**: anywhere you need `(r,
  θ)` out of `(x, y)` instead of the other way around, the umbrella case
  every item above falls under.

**Signal processing and other domains**:

* **Complex number argument**: `atan2(imaginary, real)` gives a complex
  number's phase angle.
* **I/Q demodulation in DSP**: recovering phase from in-phase/quadrature
  components.
* **Robotics**: heading-to-waypoint calculations from current position
  and target.

## A different inverse: atanh

`atan` and `atan2` both undo a function that produces *angles*. `atanh`
undoes `tanh` instead, the squashing function from the intuition
section above, and running that inverse back out has its own shape:
growing slowly near zero, then shooting toward infinity as its input
approaches ±1, the mirror image of how fast `tanh` flattens out at the
same edges.

There's no drag gesture or rotation involved here. This is the same
"inverse function" idea from `atan2`, applied to a completely different
pair of functions, and it shows up far from Android UI code, places
where a value is naturally bounded and something downstream needs it
unbounded again.

*\[Interactive demo: AtanhCurveDemo — view this post in a browser for the live version.]*

One concrete example: a correlation coefficient `r` always lands in
`(-1, 1)`, which makes it awkward to run an ordinary significance test
on directly, since those tests assume something closer to a normal
distribution. The
Fisher z-transform (A transform that maps a correlation coefficient, bounded in (-1, 1), onto an unbounded value that's approximately normally distributed, making ordinary significance tests valid on the result.)
is just `atanh(r)`:

```kotlin
import kotlin.math.atanh

// r is bounded in (-1, 1); z is not, which is the entire point.
fun fisherZ(r: Double): Double = atanh(r)

fisherZ(0.6)    // 0.693
fisherZ(0.99)   // 2.647 (close to the edge, so z grows fast here)
```

`kotlin.math.atanh` ships in the standard library and runs fine on
Android, there's just rarely a reason to reach for it in UI code, since
UI code is usually the one *producing* a bounded value (an alpha, a
progress fraction), not the one that needs to undo the bounding.

## Mental model to actually use

**An inverse function can only return what its input lets it see: `atan`
sees one collapsed ratio and has to guess a quadrant, `atan2` sees two
signed numbers and doesn't have to guess, and `atanh` sees a bounded
value and expands it back into the unbounded range `tanh` had squeezed
it out of.**


---

Canonical page: https://recomposed.blog/atan-atan2-atanh