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