View Javadoc
1   package calculator.operations;
2   
3   import java.util.List;
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  
12  /**
13   * This class represents the arithmetic operation "-".
14   * The class extends an abstract superclass Operation.
15   * Other subclasses of Operation represent other arithmetic operations.
16   * 
17   * @see Operation
18   * @see Plus
19   * @see Times
20   * @see Divides
21   */
22  public final class Minus extends Operation {
23  
24  	/**
25  	 * Class constructor specifying a number of Expressions to subtract.
26  	 *
27  	 * @param elist The list of Expressions to subtract
28  	 * @throws IllegalConstruction If an empty list of expressions if passed as
29  	 *                             parameter
30  	 * @see #Minus(List<Expression>)
31  	 */
32  	public Minus(List<Expression> elist) throws IllegalConstruction {
33  		super(elist);
34  		symbol = "-";
35  		neutral = 0;
36  	}
37  
38  	/**
39  	 * The actual computation of the (binary) arithmetic subtraction of two integers
40  	 * 
41  	 * @param l The first integer
42  	 * @param r The second integer that should be subtracted from the first
43  	 * @return The integer that is the result of the subtraction
44  	 */
45  	public int op(int l, int r) {
46  		return (l - r);
47  	}
48  
49  	/**
50  	 * The actual computation of the (binary) arithmetic subtraction of two Reals
51  	 * 
52  	 * @param r1 The first Real
53  	 * @param r2 The second Real
54  	 * @return The (new) Real that is the result of the subtraction
55  	 */
56  	public Real op(Real r1, Real r2) {
57  
58  		if ((r1.isPlusInf() && r2.isPlusInf())
59  				|| (r1.isMinusInf() && r2.isMinusInf())
60  				|| r1.isNan() || r2.isNan()) { // inf-inf = NaN or NaN + x = NaN
61  			return Real.nan();
62  		}
63  		if (r1.isMinusInf() || r2.isPlusInf()) { // x - inf = -inf
64  			return Real.minusInf();
65  		}
66  		if (r1.isPlusInf() || r2.isMinusInf()) { // x + inf = +inf
67  			return Real.plusInf();
68  		}
69  
70  		return new Real(r1.getValue().add(r2.getValue().negate()));
71  	}
72  
73  	/**
74  	 * The actual computation of the (binary) arithmetic subtraction of two
75  	 * Integers
76  	 * 
77  	 * @param i1 The first IntegerAtom
78  	 * @param i2 The second IntegerAtom
79  	 * @return The (new) IntegerAtom that is the result of the subtraction
80  	 */
81  	public IntegerAtom op(IntegerAtom i1, IntegerAtom i2) {
82  		return new IntegerAtom(i1.getValue() - i2.getValue());
83  	}
84  
85  	@Override
86  	public Complex op(Complex c1, Complex c2) {
87  		if (c1.isNaN() || c2.isNaN()) {
88  			return Complex.nan();
89  		}
90  		org.apache.commons.numbers.complex.Complex result = c1.getValue().subtract(c2.getValue());
91  		return new Complex(result);
92  	}
93  
94  	@Override
95  	public Rationnal op(Rationnal q1, Rationnal q2) {
96  		org.apache.commons.numbers.fraction.Fraction result = q1.getValue().subtract(q2.getValue());
97  		return new Rationnal(result);
98  	}
99  }