Languages
[Edit]
EN

Java - Math.tan() method example

0 points
Created by:
halfera
291

Math.tan() is a static method that takes only one parameter and returns the approximated value of the tangent mathematical function.

public class MathExample {

    public static void main(String[] args) {
        System.out.println( Math.tan( 0                  ) ); //     0 <-    0 degrees
        System.out.println( Math.tan( 0.7853981633974483 ) ); //    ~1 <-  ~45 degrees ==  PI / 4
        System.out.println( Math.tan( 1.5707963267948966 ) ); // ~+Inf <-  ~90 degrees ==  PI / 2

        System.out.println( Math.tan(-0.7853981633974483 ) ); //   ~-1 <- ~-45 degrees == -PI / 4
        System.out.println( Math.tan(-1.5707963267948966 ) ); // ~-Inf <- ~-90 degrees == -PI / 2
    }
}

Note: 0.9999999999999999, 16331239353195370, -0.9999999999999999 and -16331239353195370 should be equal to 1+Inf, -1 and -Inf but they are not because of compuptation precision error.


1. Documentation

Syntax
package java.lang;

public final class Math {

    public static double tan(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.tan() call.

Parametersnumber - double value in radians (primitive value).
Resultnumber value calculated as tan(x) mathematical function (primitive value).
DescriptionMath.tan() is a static method that takes only one parameter and returns the approximated value of tangent mathematical function.

2. Working with radians

public class MathExample {

    public static void main(String[] args) {
        double x1 = 0.0;          // beginning of calculation in radians
        double x2 = Math.PI / 2;  // ending of calculation radians

        double dx = Math.PI / 9; // calculation step in degrees

        for (double rad = x1; rad <= x2; rad += dx) {
            double y = Math.tan(rad);

            System.out.println("tan(" + rad + " rad) = " + y);
        }
    }
}

Output: 

tan(0.0 rad) = 0.0
tan(0.3490658503988659 rad) = 0.36397023426620234
tan(0.6981317007977318 rad) = 0.8390996311772799
tan(1.0471975511965976 rad) = 1.7320508075688767
tan(1.3962634015954636 rad) = 5.671281819617707

3. Working with degrees

public class MathExample {

    static double calculateTan(double deg) {
        double radians = (Math.PI / 180) * deg;

        return Math.tan(radians);
    }

    public static void main(String[] args) {
        // Example:
        double x1 = 0.0;  // beginning of calculation in degrees
        double x2 = 90.0; // ending of calculation degrees

        double dx = 30.0;  // calculation step in degrees

        for (double deg = x1; deg  <= x2; deg  += dx) {
            double y = calculateTan(deg );

            System.out.println("tan(" + deg + " deg) = " + y);
        }
    }
}

Output: 

tan(0.0 deg) = 0.0
tan(30.0 deg) = 0.5773502691896257
tan(60.0 deg) = 1.7320508075688767
tan(90.0 deg) = 1.633123935319537E16

4. Reversed console plot example

public class CustomMath {

    static void printLine(double y1, double y2, double dy, char character) {
        StringBuilder line = new StringBuilder("");

        for (double y = y1; y < y2; y += dy) {
            line.append(" ");
        }

        System.out.println(line.append(character));
    }

    public static void main(String[] args) {
        System.out.println();
        double x1 = -3.14 * 2; // begining of sine chart
        double x2 = +3.14 * 2; // end of sine chart

        double y1 = -4.0;
        double y2 = +4.0;

        double xSteps = 60;
        double ySteps = 60;

        double dx = (x2 - x1) / xSteps; // x axis step
        double dy = (y2 - y1) / ySteps; // y axis step

        for (double rad = x1; rad < x2; rad += dx) {
            double y = Math.tan(rad);

            if (y <= y1 || y >= y2) {
                System.out.println(" ");
            } else {
                printLine(y1, y, dy, 'x');
            }
        }
    }
}

Output:

                               x
                                x
                                  x
                                    x
                                       x
                                            x
                                                      x


        x
                  x
                      x
                         x
                           x
                             x
                               x
                                x
                                  x
                                    x
                                       x
                                            x
                                                      x


       x
                  x
                      x
                         x
                           x
                             x
                               x
                                x
                                  x
                                    x
                                       x
                                           x
                                                      x


       x
                 x
                      x
                         x
                           x
                             x
                              x
                                x
                                  x
                                    x
                                       x
                                           x
                                                     x


       x
                 x
                      x
                         x
                           x
                             x

See also

  1. Java - Math.atan() method example
  2. Java - Math.atan2() method example

References

  1. 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