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 "tan".
15   * The class extends an abstract superclass UnaryFunction.
16   */
17  public final class Tangente extends UnaryFunction {
18  
19  	/**
20  	 * Class constructor specifying an Expression to apply the tangent function.
21  	 *
22  	 * @param arg The Expression to apply the operation on
23  	 * @throws IllegalConstruction If a null argument is passed
24  	 */
25  	public Tangente(Expression arg) throws IllegalConstruction {
26  		super(arg);
27  		symbol = "tan";
28  	}
29  
30  	@Override
31  	public Real op(Real r) {
32  		if (r.isNan() || r.isMinusInf() || r.isPlusInf()) {
33  			return Real.nan();
34  		}
35  		BigDecimal val = BigDecimalMath.tan(r.getValue(), Real.context);
36  		return new Real(val);
37  	}
38  
39  	@Override
40  	public IntegerAtom op(IntegerAtom i) {
41  		double tanVal = Math.tan(i.getValue());
42  		return new IntegerAtom((int) Math.round(tanVal));
43  	}
44  
45  	@Override
46  	public Complex op(Complex c) {
47  		org.apache.commons.numbers.complex.Complex val = c.getValue().tan();
48  		return new Complex(val);
49  	}
50  
51  	@Override
52  	public Rationnal op(Rationnal q) {
53  		double tanVal = Math.tan(q.getValue().doubleValue());
54  		org.apache.commons.numbers.fraction.Fraction result = org.apache.commons.numbers.fraction.Fraction.from(tanVal);
55  		return new Rationnal(result);
56  	}
57  }