View Javadoc
1   package calculator.functions;
2   
3   import java.math.BigDecimal;
4   
5   import calculator.Expression;
6   import calculator.IllegalConstruction;
7   import calculator.atoms.Complex;
8   import calculator.atoms.IntegerAtom;
9   import calculator.atoms.Rationnal;
10  import calculator.atoms.Real;
11  import ch.obermuhlner.math.big.BigDecimalMath;
12  
13  /**
14   * This class represents the arithmetic unary operation "cosh".
15   * The class extends an abstract superclass UnaryOperation.
16   */
17  public final class Cosh extends UnaryFunction {
18  
19  	/**
20  	 * Class constructor specifying an Expression to apply the hyperbolic cosine
21  	 * function.
22  	 *
23  	 * @param arg The Expression to apply the operation on
24  	 * @throws IllegalConstruction If a null argument is passed
25  	 */
26  	public Cosh(Expression arg) throws IllegalConstruction {
27  		super(arg);
28  		symbol = "cosh";
29  	}
30  
31  	@Override
32  	public Real op(Real r) {
33  		if (r.isNan() || r.isMinusInf() || r.isPlusInf()) {
34  			return Real.nan();
35  		}
36  
37  		BigDecimal val = BigDecimalMath.cosh(r.getValue(), Real.context);
38  		return new Real(val);
39  	}
40  
41  	@Override
42  	public IntegerAtom op(IntegerAtom i) {
43  		double sinVal = Math.cosh(i.getValue());
44  		return new IntegerAtom((int) Math.round(sinVal));
45  	}
46  
47  	@Override
48  	public Complex op(Complex c) {
49  		org.apache.commons.numbers.complex.Complex val = c.getValue().cosh();
50  		return new Complex(val);
51  	}
52  
53  	@Override
54  	public Rationnal op(Rationnal q) {
55  		double cosVal = Math.cosh(q.getValue().doubleValue());
56  		org.apache.commons.numbers.fraction.Fraction result = org.apache.commons.numbers.fraction.Fraction.from(cosVal);
57  		return new Rationnal(result);
58  	}
59  }