Table of Contents

    Mastering the atan() Method in Java: A Comprehensive Guide

    Description

    On this document we will be showing a java example on how to use the atan(double a) method of Math Class. The atan(double a) returns the arc tangent of the method argument value. The returned angle is in the range -pi/2 through pi/2.. The following rule must be noted

     

    • If the argument is NaN or its absolute value is greater than 1, then the result is NaN.
    • If the argument is zero, then the result is a zero with the same sign as the argument.

    The arc tangent of value is basically just the inverse tangent of a value. To further illustrate this below is the equation.

    atan(a) = tan-1a

    Most of the methods of the Math class is static and the atan() method is no exception. Thus don’t forget that in order to call this method, you don’t have to create a new object. Use the method in the format Math.atan(a).

    The method returns the arctangent of the specified double value.

    Syntax

    double atan(double d)

    Parameters

    Here is the detail of parameters ?

    • d ? A double data type. The value whose arc tangent is to be returned.

    Return Value

    • This method returns the arctangent of the specified double value. 

    Example

    public class MathATan {
    
       public static void main(String args[]) {
          double degrees = 45.0;
          double radians = Math.toRadians(degrees);
    
          System.out.format("The value of pi is very approximately %.4f%n", Math.PI);
          System.out.format("The arctangent of %.4f is degrees ", Math.atan(radians) );
       }
    }

    Output

    The value of pi is very approximately 3.1416
    The arctangent of 0.6658 is degrees Press any key to continue . . .

    Example

    Below is a java code demonstrates the use of atan() method of Math class. The example presented might be simple however it shows the behavior of the atan() method.

    import java.util.Scanner;
    
    /*
     * This example source code demonstrates the use of
     * atan() method of Math class
     */
    
    public class MathAtanExample {
    
    	public static void main(String[] args) {
    
    		// Ask for user input
    		System.out.print("Enter a value:");
    
    		// use scanner to read the console input
    		Scanner scan = new Scanner(System.in);
    
    		// Assign the user to String variable
    		String s = scan.nextLine();
    
    		// close the scanner object
    		scan.close();
    
    		// get the arc tangent value of the user input
    		double atanValue = Math.atan(Double.parseDouble(s));
    		System.out.println("arc tangent of " + s + " is " + atanValue);
    
    	}
    
    }

    Output

    Enter a value:1.45
    arc tangent of 1.45 is 0.9670469933974603
    Press any key to continue . . .

    The above java example source code demonstrates the use of atan() method of Math class. We simply ask for user input and we use the Scanner class to parse it. Since we have used the nextLine() method to get the console value, and the return data type is String thus we have used the Double.parseDouble() to transform it into double. We have to convert it first to double because the atan() method accepts double method argument.