Errors in equals Errors in negate Errors in add Errors in


ERRORS IN EQUALS: A few submitted solutions used this to test whether to Lists
are equal:

this.bigint == b.bigint

This is wrong. It tests whether the names of the two instances are the same, not
whether the lists have the same contents.. To do that, you need:

equals(this.bigint, b.bigint)



ERRORS IN NEGATE: The following code is wrong, because it changes instance this.

public BigInt negate() { BigInt res= this; if(res.sign && !res.equals( new
BigInt(0))) res.sign= false; else res.sign= true; return res; }

res and this contain the same name; change one instance and you change the
other. As an example,

BigInt x= y.negate();

changes y's sign as well as storing the negation of the original y in x.

The assignment handout says clearly in one place that these methods should not
change the inputs to them; they are to be purely functional.


ERRORS IN SUBTRACT

The following method is wrong because it changes instance b. See the comment on
method negate

    public BigInt subtract(BigInt b) {
    	BigInt temp = b;
    	temp.sign = !temp.sign; // a - b = a + (-b)
    	return this.add(temp);
    }


ERRORS IN FACT

Our testing program sometimes said that FACT produced the right result, even
though, internall, it was wrong:

Testing fact(10) didn't work. Your result was: 3628800. Points deducted: 8

We ran the testing program using base 2, and in this base, your program may have
produced the correct value 3628800 but still been wrong. Many of the programs
produce this bigint:

      (0, 0, 0, 0, 0, 0, 0, 0, 1, 7087)
      
When the correct bigint is:

      (0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1)

The wrong bigints have the weird digit 7087 at the end, even though this is bas 2.

The problem is that method fact contains the method call

      mult(x,i).
      
The specification of method mult(x,i) clearly states the precondition 0 <= i < BASE.
So if i >= BASE, mult should not be called. It is this bad call on mulot(x,i) that
produces an incorrect internal representation.

Instance method mult should have been used instead.