-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTangent.java
More file actions
38 lines (28 loc) · 1.03 KB
/
Tangent.java
File metadata and controls
38 lines (28 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package ru.itmo.qa.lab2.trig;
import ru.itmo.qa.lab2.function.AbstractFunction;
import java.math.BigDecimal;
import static java.lang.String.format;
import static java.math.RoundingMode.HALF_EVEN;
public class Tangent extends AbstractFunction {
private final Sine sine;
private final Cosine cosine;
public Tangent() {
super();
this.sine = new Sine();
this.cosine = new Cosine();
}
public Tangent(Sine sine, Cosine cosine) {
this.sine = sine;
this.cosine = cosine;
}
@Override
public BigDecimal calculate(BigDecimal x, BigDecimal precision) throws ArithmeticException {
isValid(x, precision);
BigDecimal sin = sine.calculate(x, precision.setScale(precision.scale() + 5, HALF_EVEN));
BigDecimal cos = cosine.calculate(x, precision.setScale(precision.scale() + 5, HALF_EVEN));
if (cos.abs().compareTo(precision) < 0) {
throw new ArithmeticException(format("У тангенса нет значения при x = %s", x));
}
return sin.divide(cos, precision.scale(), HALF_EVEN);
}
}