-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSine.java
More file actions
50 lines (38 loc) · 1.31 KB
/
Sine.java
File metadata and controls
50 lines (38 loc) · 1.31 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
39
40
41
42
43
44
45
46
47
48
49
50
package ru.itmo.qa.lab2.trig;
import ch.obermuhlner.math.big.BigDecimalMath;
import ru.itmo.qa.lab2.function.AbstractFunction;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
public class Sine extends AbstractFunction {
public Sine() {
super();
}
@Override
public BigDecimal calculate(BigDecimal x, BigDecimal precision) throws ArithmeticException {
isValid(x, precision);
MathContext mc = new MathContext(precision.scale() + 10, RoundingMode.HALF_EVEN);
BigDecimal pi = BigDecimalMath.pi(mc);
BigDecimal tau = pi.multiply(BigDecimal.valueOf(2));
x = x.remainder(tau);
if (x.compareTo(pi) > 0) {
x = x.subtract(tau);
} else if (x.compareTo(pi.negate()) < 0) {
x = x.add(tau);
}
BigDecimal result = x;
BigDecimal term = x;
BigDecimal x2 = x.multiply(x, mc);
int i = 1;
do {
term = term.multiply(x2, mc)
.divide(BigDecimal.valueOf((2L * i) * (2L * i + 1)), mc);
result = result.add(term.multiply(minusOnePower(i)), mc);
i++;
} while (term.abs().compareTo(precision.divide(BigDecimal.TEN, mc)) > 0);
return result.setScale(precision.scale(), RoundingMode.HALF_EVEN);
}
private static BigDecimal minusOnePower(int n) {
return BigDecimal.valueOf(1L - (n % 2) * 2);
}
}