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.
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.
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.
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");
}
}
}
Output
Ramp: rise 3 per run 4 -> 36.87 degrees
Roof: rise 6 per run 12 -> 26.57 degrees
Road: rise 1 per run 10 -> 5.71 degrees
How It Works
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.
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.
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.
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.
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.
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.