EN
Java - Math.tan() method example
0
points
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 to1
,+Inf
,-1
and-Inf
but they are not because of compuptation precision error.
1. Documentation
Syntax |
|
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. |
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