Languages
[Edit]
EN

Java - Math.atan() method example

0 points
Created by:
Brett4
285

The Math.atan function returns number in radians in the range -Math.PI/2 to +Math.PI/2. The function calculates the inverted tangent function value.

public class CustomMath {

    static double calculateAngle(double a, double b) {
        return Math.atan(a / b);
    }
    
    public static void main(String[] args) {
        /*
          |\
          | \ h
        a |  \
          |__*\ <- angle
            b
        */

        double a, b;

        // a an b build isosceles right triangle
        a = 3;
        b = a;
        System.out.println( calculateAngle(a, b) ); // 0.7853981633974483 <- ~45 degrees

        // a and b build half of equilateral triangle
        a = 3;
        b = a * Math.sqrt(3);
        System.out.println( calculateAngle(a, b) ); // 0.5235987755982988 <- ~30 degrees

        // a and b build very high (+Inf) and slim (~0) triangle
        a = Double.POSITIVE_INFINITY;
        b = 0;
        System.out.println( calculateAngle(a, b) ); // 1.5707963267948966 <- ~90 degrees
    }
}

1. Documentation

Syntax
package java.lang;

public final class Math {

    public static double atan(double number) { ... }

}

Note: Classes in the java.lang package are imported automatically, so it is not necessary to do it manually - we use just Math.atan() call.

Parameters

number - double number value that represents the result of the operation opposite / adjacent on the right triangle (primitive value).

Result

number value in radians in the range -Math.PI/2 to +Math.PI/2 (primitive value).

If value can not be calculated NaN is returned.

Description

atan is a static method that takes only one parameter and returns an approximation of the result of the mathematical function arctangent(x).

2. Working with degrees

public class CustomMath {

    static double calculateAngle(double a, double b) {
        double angle = Math.atan(a / b);

        return (180 / Math.PI) * angle; // rad to deg conversion
    }
    
    public static void main(String[] args) {
        /*
          |\
          | \ h
        a |  \
          |__*\ <- angle
            b
        */

        double a, b;

        // a an b build isosceles right triangle
        a = 3;
        b = a;
        System.out.println( calculateAngle(a, b) ); // ~45 degrees

        // a and b build half of equilateral triangle
        a = 3;
        b = a * Math.sqrt(3);
        System.out.println( calculateAngle(a, b) ); // ~30 degrees

        // a and b build very high (+Inf) and slim (~0) triangle
        a = Double.POSITIVE_INFINITY;
        b = 0;
        System.out.println( calculateAngle(a, b) ); // ~90 degrees
    }
}

References

  1. Inverse trigonometric functions - Wikipedia

Java - Math object

Native Advertising
ūüöÄ
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

‚̧ԳŹūüíĽ ūüôā

Join