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
14
15
16
17
18
19
20
21
22 public final class Minus extends Operation {
23
24
25
26
27
28
29
30
31
32 public Minus(List<Expression> elist) throws IllegalConstruction {
33 super(elist);
34 symbol = "-";
35 neutral = 0;
36 }
37
38
39
40
41
42
43
44
45 public int op(int l, int r) {
46 return (l - r);
47 }
48
49
50
51
52
53
54
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()) {
61 return Real.nan();
62 }
63 if (r1.isMinusInf() || r2.isPlusInf()) {
64 return Real.minusInf();
65 }
66 if (r1.isPlusInf() || r2.isMinusInf()) {
67 return Real.plusInf();
68 }
69
70 return new Real(r1.getValue().add(r2.getValue().negate()));
71 }
72
73
74
75
76
77
78
79
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 }