astropy/astropy — pull request #1254 by mhvk (2013-10-07 19:32:54+00:00)
Title: Quantities should always allow comparison with zero

Also while working on #929 (where I needed to intercept a `where(q==0,...)`, I noticed that comparisons of the form `q==0`, `q>0` raise an exception unless `q` is dimensionless. They probably shouldn't as this has a well-defined answer (as 0 can have any unit it wants!). Since quantity is so much in flux right now, I won't do a PR, but just note that one way of simplifying this is by defining a difference function which returns a plain array for testing in the various comparisons. E.g., things can be made to work with.
```
def _diff(self, other, raise_on_incompatible=True):
try:
return self.value - other.to(self.unit).value
except AttributeError:
if np.all(other*0. == other):
return self.value
try:
return self.to(1).value - other
except(UnitsException,TypeError):
if raise_on_incompatible:
raise TypeError("Quantity object cannot be compared to an "
"object of type {0}"
.format(other.__class__))
except UnitsException:
if raise_on_incompatible:
raise UnitsException("Can only compare quantities with "
"compatible dimensions.")
return False
```
(the `raise_on_incompatible=False` option is there for `==`, which should simply return `False` in this case).