0

I understand that there are many answers to this question but I'm still having difficulty with this. I understand the idea of A > B, B > C, so A > C and I believe that what I'm doing is capable of working perfectly fine.

public int compareTo(Snake snake) {
    double other=snake.testFitness();
    double tFit = this.testFitness();
    if(tFit < other)
        return 1;
    else if(tFit > other)
        return -1;
    else
        return 0;
}

I don't understand how something so simple is able to violate the general contract.

Edit: For those asking for the testFitness() function.

public double testFitness() {
    fit += ((this.length - 3)*2);
    fit -= this.blocksTraveled*3;
    return fit;
}
MagnusCaligo
  • 707
  • 2
  • 8
  • 28
  • Who says you're violating the contract? – shmosel Jun 23 '16 at 02:35
  • *I believe that what I'm doing is capable of working perfectly fine.* I believe there is something incorrect in that belief, else you wouldn't have posted this. Unfortunately, I can't quite work out what your question is. Could you elaborate? – Elliott Frisch Jun 23 '16 at 02:35
  • @shmosel Java does when I try to run my program for too long :P – MagnusCaligo Jun 23 '16 at 02:36
  • Does `Snake.testFitness()` return a consistent value? – shmosel Jun 23 '16 at 02:36
  • Yes, Snake.testFitness() is always the same – MagnusCaligo Jun 23 '16 at 02:36
  • Post that method please. – shmosel Jun 23 '16 at 02:36
  • The reason why I thought that what I was doing should work perfectly fine is because some of the other answers that I have found would be missing some information. I.E. they would forget that if the values were equal the function needs to return 0, etc. – MagnusCaligo Jun 23 '16 at 02:37
  • 2
    I just... I just... figured it out.... +=.... – MagnusCaligo Jun 23 '16 at 02:38
  • 1
    You should also replace your comparison with `Double.compare(other, tFit)` or you risk running into trouble with floating-point precision. – shmosel Jun 23 '16 at 02:40
  • @MagnusCaligo Rather than beating yourself up about this, chalk it up to learning. More importantly, ANSWER YOUR OWN QUESTION with a description of the problem. After the required delay, accept the answer. You can be sure someone else will find it useful at some point in the future, which is the ***whole point of StackOverflow***. – Jim Garrison Jun 23 '16 at 02:54
  • Voted to close as a duplicate since this is a nontransitive comparison – Nathan Hughes Jun 23 '16 at 02:54
  • @shmosel That is not correct; there is no problem with floating-point precision. If you look at the code for `Double.compare`, it starts out `if (d1 < d2) return -1; if (d1 > d2) return 1;`, same as what OP's code is doing. After that, there's some special code, but not because of precision; the problem is NaNs. If there is no possibility of NaN, then OP's code is equivalent to using `Double.compare`. – ajb Jun 23 '16 at 03:39
  • This duplicate question is probably the wrong one. There's nothing wrong with the logic of `compareTo` _per se_; the problem is that it doesn't return consistent results given the same objects, because the comparison calls a method that has side effects. – ajb Jun 23 '16 at 03:51

0 Answers0