[Mono-list] Int32.CompareTo() bug

Nick Drochak ndrochak@gol.com
Thu, 29 Nov 2001 00:13:20 +0900


Miguel,

Int32.CompareTo(object v) has a bug.  Your solution is to return the
difference (which seems obvious):
	return value - (int) v;

However, when value and/or v are close to (or equal) MaxValue and
MinValue respectively, the arithmetic causes the bits to roll over and
produce strange results. For example, try it with
MaxValue.CompareTo(MinValue).  Your method returns zero.

I struggled to come up with a solution to this but couldn't before I got
too tired, or because I was too tired...yeah, that's it.

Oh yeah, and bug #2.  The docs say that if v is null, CompareTo() should
return a value greater than zero.  It seems that you throw an exception.


Regards,
Nick D.

P.S. Thanks to Mario for writing the test that caught this.