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