Java Math atan() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.lang.Math

Definition

In Java, Math.atan(x) returns the arc tangent of x: the angle, in radians, whose tangent is x. It undoes Math.tan. Math.atan(1) is 0.7853981633974483, which is PI/4 (45 degrees).

Unlike asin and acos, it accepts any number, so it never returns NaN for a normal input. Use it to turn a slope (rise / run) into an angle. The result is always between -PI/2 and PI/2, so it cannot tell opposite directions apart: for that, use Math.atan2.

A static method of the java.lang.Math class, available since the early versions of Java (JDK 1.0). Call it on the class name, as in Math.atan(...). No import is needed, because classes in java.lang are always visible.

Syntax

Pass any double. The angle comes back in radians.

Java
double a = Math.atan(1.0);                        // 0.7853981633974483
double b = Math.atan(0.0);                        // 0.0
double deg = Math.toDegrees(Math.atan(rise / run));
double limit = Math.atan(Double.POSITIVE_INFINITY);  // PI/2
CallWhat it does
Math.atan(1.0)Returns PI/4 (0.7853981633974483).
Math.atan(-1.0)Returns -PI/4.
Math.atan(Double.POSITIVE_INFINITY)Returns PI/2 (1.5707963267948966).
Math.toDegrees(Math.atan(0.75))About 36.87 degrees.

There is only a double version. Division by zero in double arithmetic gives Infinity instead of an exception, so Math.atan(3 / 0.0) is simply PI/2.

Library declarations
Java
// java.lang.Math
public static double atan(double a)

Parameters

ParameterDescription
aAny value, usually a slope rise / run. Infinity is allowed.

Return Value

Return ValueDescription
doubleThe angle in radians, in the range -PI/2 to PI/2. The result is NaN only if a is NaN. For a zero argument the result is a zero with the same sign.

Compare with Similar Methods

Finding angles in Java:

MethodHow it differs
atan2(y, x)Takes both sides, so it knows the quadrant; range -PI to PI.
tan(a)The forward function that atan reverses.
asin(x)Angle from opposite / hypotenuse; domain limited to -1..1.
toDegrees(rad)Turns the angle into degrees.

Java Examples

Example 1 — Arc Tangent at 0, 1, -1, and Infinity

The tangent of an angle can be any number, so atan accepts any number and squeezes it into -PI/2 to PI/2.

Java
import java.util.Locale;

public class MathAtanBasics {
    public static void main(String[] args) {
        System.out.println("atan(0.0)  = " + Math.atan(0.0));
        System.out.println("atan(1.0)  = " + Math.atan(1.0));
        System.out.println("atan(-1.0) = " + Math.atan(-1.0));
        System.out.println("atan(Infinity)  = " + Math.atan(Double.POSITIVE_INFINITY));
        System.out.println("atan(-Infinity) = " + Math.atan(Double.NEGATIVE_INFINITY));
        System.out.println("atan(1000000)   = " + String.format(Locale.US, "%.4f", Math.atan(1000000)));
    }
}

How It Works

  1. 45 degrees both ways. A slope of 1 means rise equals run, which is PI/4 radians; a slope of -1 is the same angle below the horizontal.
  2. The limits. An infinite slope is a vertical line, so the angle approaches (and for Infinity reaches) a quarter turn: 1.5707963267948966 in either direction.
  3. Close to the limit. Even a slope of one million is below it, but at four decimals 1.5708 is already the quarter turn, showing how flat the curve becomes for huge inputs.

Example 2 — Turn Rise and Run Into an Angle

A wheelchair ramp, a roof, and a road: each is described by a rise and a run, and atan gives the angle.

Java
import java.util.Locale;

public class MathAtanSlope {
    public static void main(String[] args) {
        String[] names = {"Ramp", "Roof", "Road"};
        int[][] slopes = {{3, 4}, {6, 12}, {1, 10}};
        for (int i = 0; i < slopes.length; i++) {
            double ratio = (double) slopes[i][0] / slopes[i][1];
            double degrees = Math.toDegrees(Math.atan(ratio));
            System.out.println(names[i] + ": rise " + slopes[i][0] + " per run " + slopes[i][1]
                    + " -> " + String.format(Locale.US, "%.2f", degrees) + " degrees");
        }
    }
}

How It Works

  1. Cast before dividing. (double) slopes[i][0] makes the division 3 / 4 = 0.75; plain int division would give 0 and every angle would collapse to 0.
  2. A 6-in-12 roof. The ratio 0.5 gives about 26.57 degrees, the well-known 6/12 roof pitch; the ratio 0.75 of the ramp gives 36.87 degrees.
  3. Gentle slopes. A 10 percent grade (rise 1 per run 10) is only 5.71 degrees, much less than 10 degrees: for small slopes the tangent is slightly larger than the angle in radians.

Example 3 — Why atan Cannot Tell Left From Right

The point (-4, 3) is up and to the left, but atan(3 / -4) only sees the ratio -0.75. Compare with atan2.

Java
import java.util.Locale;

public class MathAtanQuadrant {
    public static void main(String[] args) {
        double rise = 3.0;
        double runRight = 4.0;
        double runLeft = -4.0;
        System.out.println("atan, right: " + String.format(Locale.US, "%.2f", Math.toDegrees(Math.atan(rise / runRight))));
        System.out.println("atan, left:  " + String.format(Locale.US, "%.2f", Math.toDegrees(Math.atan(rise / runLeft))));
        System.out.println("atan2, left: " + String.format(Locale.US, "%.2f", Math.toDegrees(Math.atan2(rise, runLeft))));
        System.out.println("atan, run 0: " + String.format(Locale.US, "%.2f", Math.toDegrees(Math.atan(rise / 0.0))));
    }
}

How It Works

  1. Right side is fine. Rise 3 and run 4 give the ratio 0.75, and 36.87 degrees is the correct angle for a point up and to the right.
  2. Left side is wrong. With run -4 the ratio becomes -0.75, so atan answers -36.87 degrees (down and to the right) for a point that is really up and to the left.
  3. atan2 and the vertical case. Math.atan2(3, -4) sees both signs and returns 143.13 degrees; separately, a run of 0.0 makes the ratio Infinity, which atan still turns into 90.00 degrees.

Frequently Asked Questions

It returns the arc tangent of a number: the angle in radians, between -PI/2 and PI/2, whose tangent equals that number. Math.atan(1) is 0.7853981633974483.
atan takes one ratio and cannot tell which quadrant the angle is in. atan2(y, x) takes both coordinates and returns an angle from -PI to PI in the correct quadrant.
Math.atan(Double.POSITIVE_INFINITY) is PI/2 (1.5707963267948966) and Math.atan(Double.NEGATIVE_INFINITY) is -PI/2. Only a NaN input gives NaN.
Divide rise by run as doubles and call Math.toDegrees(Math.atan(rise / run)). For example a rise of 3 over a run of 4 gives about 36.87 degrees.
No. Unlike asin and acos it has no restricted domain, so every ordinary number and both infinities give a valid angle.

Next: atan2()

The angle of the point (x, y) in radians, from -pi to pi, in the correct quadrant.

atan2() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful