How do you compare structs for equality in C?
C provides no language facilities to do this – you have to do it yourself and compare each structure member by member.
C provides no language facilities to do this – you have to do it yourself and compare each structure member by member.
Start with NSUInteger prime = 31; NSUInteger result = 1; Then for every primitive you do result = prime * result + var For objects you use 0 for nil and otherwise their hashcode. result = prime * result + [var hash]; For booleans you use two different values result = prime * result + … Read more
In Python 2.x this is not guaranteed as it is possible for True and False to be reassigned. However, even if this happens, boolean True and boolean False are still properly returned for comparisons. In Python 3.x True and False are keywords and will always be equal to 1 and 0. Under normal circumstances in … Read more
You would not want the language to automatically rewrite a != b as !(a == b) when a == b returns something other than a bool. And there are a few reasons why you might make it do that. You may have expression builder objects, where a == b doesn’t and isn’t intended to perform … Read more
Use the static Double.isNaN(double) method, or your Double‘s .isNaN() method. // 1. static method if (Double.isNaN(doubleValue)) { … } // 2. object’s method if (doubleObject.isNaN()) { … } Simply doing: if (var == Double.NaN) { … } is not sufficient due to how the IEEE standard for NaN and floating point numbers is defined.
You should implement the method __eq__: class MyClass: def __init__(self, foo, bar): self.foo = foo self.bar = bar def __eq__(self, other): if not isinstance(other, MyClass): # don’t attempt to compare against unrelated types return NotImplemented return self.foo == other.foo and self.bar == other.bar Now it outputs: >>> x == y True Note that implementing __eq__ … Read more
Consider this simple problem: class Number: def __init__(self, number): self.number = number n1 = Number(1) n2 = Number(1) n1 == n2 # False — oops So, Python by default uses the object identifiers for comparison operations: id(n1) # 140400634555856 id(n2) # 140400634555920 Overriding the __eq__ function seems to solve the problem: def __eq__(self, other): “””Overrides … Read more
I’m going to heavily quote the Object documentation here, because I think it has some great explanations. I encourage you to read it, and also the documentation for these methods as they’re overridden in other classes, like String. Side note: if you want to try these out for yourself on different objects, use something like … Read more
For all built-in Python objects (like strings, lists, dicts, functions, etc.), if x is y, then x==y is also True. Not always. NaN is a counterexample. But usually, identity (is) implies equality (==). The converse is not true: Two distinct objects can have the same value. Also, is it generally considered better to just use … Read more
is will return True if two variables point to the same object (in memory), == if the objects referred to by the variables are equal. >>> a = [1, 2, 3] >>> b = a >>> b is a True >>> b == a True # Make a new copy of list `a` via the … Read more