Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static BigDecimal cosine(BigDecimal x) {
- final int SCALE = 18;
- final int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;
- BigDecimal currentValue = BigDecimal.ONE;
- BigDecimal lastVal = currentValue.add(BigDecimal.ONE);
- BigDecimal xSquared = x.multiply(x);
- BigDecimal numerator = BigDecimal.ONE;
- BigDecimal denominator = BigDecimal.ONE;
- int i = 0;
- while(lastVal.compareTo(currentValue) != 0) {
- lastVal = currentValue;
- int z = 2 * i + 2;
- denominator = denominator.multiply(BigDecimal.valueOf(z));
- denominator = denominator.multiply(BigDecimal.valueOf(z - 1));
- numerator = numerator.multiply(xSquared);
- BigDecimal term = numerator.divide(denominator, SCALE + 5, ROUNDING_MODE);
- if(i % 2 != 0) {
- currentValue = currentValue.add(term);
- } else {
- currentValue = currentValue.subtract(term);
- }
- i++;
- }
- return currentValue;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement