Table of Contents

    Understanding the tan() Method in Java: A Comprehensive Guide

    Description

    On this tutorial, we will be showing a java example on how to use the tan(double a) method of Math Class. The tan(double a) returns the trigonometric tangent of the method argument value. It must be noted that the parameter is in radians, however, if your source value is in the degree which is always the case for real world applications the handy Math.toRadians() method will be helpful for the conversion. The following rule must be noted:

    • If the argument is NaN or an infinity, then the result is NaN.
    • If the argument is zero, then the result is a zero with the same sign as the argument.

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

    The method returns the tangent of the specified double value.

    Syntax

    double tan(double d)

    Parameters

    Here is the detail of parameters ?

    • d ? A double data type.

    Return Value

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

    Example

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

    Output

    The value of pi is 3.1416
    The tangent of 45.0 degrees is 1.0000
    Press any key to continue . . .

    Example

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

    import java.util.Scanner;
    
    /*
     * This example source code demonstrates the use of
     * tan() method of Math class
     */
    
    public class MathTangentExample {
    
    	public static void main(String[] args) {
    
    		// Ask for user input
    		System.out.print("Enter an angle in degrees:");
    
    		// 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();
    
    		// convert the string input to double
    		double value = Double.parseDouble(s);
    		// convert the value to radians
    		double valueRadians = Math.toRadians(value);
    
    		// get the tangent of the angle
    		double tangentValue = Math.tan(valueRadians);
    		System.out.println("Tangent of " + s + " is " + tangentValue);
    
    	}
    
    }

    Output

    Enter an angle in degrees:45
    Tangent of 45 is 0.9999999999999999
    Press any key to continue . . .

    The above java example source code demonstrates the use of tan() 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 tan() method accepts double method argument. After transforming into double we have also used Math.toRadians() to convert the degree input to radians which is the required method argument.