Home Communities
IT Knowledge
Inspiration
Languages
EN

# Java - Math.tan() method example

0 points
Created by:
351

`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. Parameters `number` - double value in radians (primitive value). Result `number` value calculated as `tan(x)` mathematical function (primitive value). Description `Math.tan()` is a static method that takes only one parameter and returns the approximated value of tangent mathematical function.

``````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

}
}
}``````

Output:

``````tan(0.0 rad) = 0.0

## 3. Working with degrees

``````public class MathExample {

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

}

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

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``````

## References

1. Trigonometric functions - Wikipedia

Join to our subscribers to be up to date with content, news and offers.