equality

String comparison in Python: is vs. ==

I noticed a Python script I was writing was acting squirrelly, and traced it to an infinite loop, where the loop condition was "while line is not ''". Running through it in the debugger, it turned out that line was in fact ''. When I changed it to != rather than 'is not', it worked fine. I did some searching, and found this question, t...

When is it necessary to override Equals() in a .NET class?

.NET contains its own equality comparison functionality, however I don't really understand how it works. If the desired Equals() and == behaviour is to verify that every field of an object is equal to every field of another object, is it necessary to override Equals() with a method that does this explicitly? ...

why assert_equal() in Ruby on Rails sometimes seem to compare by Identity and sometimes by value?

it was very weird that yesterday, I was do an integration test in Rails and assert_equal array_of_obj1, array_of_obj2 # obj1 from db, obj2 created in test and it failed. The values shown inside the array and objects were identical. If I change the test to assert array_of_obj1 == array_of_obj2 Then it will pass. But today, th...

floating point equality in Python and in general

I have a piece of code that behaves differently depending on whether I go through a dictionary to get conversion factors or whether I use them directly. The following piece of code will print 1.0 == 1.0 -> False But if you replace factors[units_from] with 10.0 and factors[units_to ] with 1.0 / 2.54 it will print 1.0 == 1.0 -> True #!...

How to test pair wise equality of two collections

How do I test whether two collections are equal as according each pair of elements being equal according to .Equals()? I find myself writing a little function (given below) which seems over the top. I imagine there must be a far simpler way to do this. bool ListsEqual<T>(IList<T> lhs, IList<T> rhs) where T : IEquatable<T> { if (lh...

Testing equality of DataColumn values in C#

Hi all I've written some code to test equality between column values in DataTables when the column type isn't known. Testing directly like this: row["Foo"] == row["Bar"] always results in false, presumably because object's implementation of Equals uses ReferenceEquals. So I've resorted to: row["Foo"].ToString() == row["Bar"].ToStr...

Can ( s is "" ) and ( s == "" ) ever give different results in Python 2.6.2?

As any Python programmer knows, you should use == instead of is to compare two strings for equality. However, are there actually any cases where ( s is "" ) and ( s == "" ) will give different results in Python 2.6.2? I recently came across code that used ( s is "" ) in code review, and while pointing out that this was incorrect I wante...

jQuery object equality

How do I determine if two jQuery objects are equal? I would like to be able to search an array for a particular jQuery object. $.inArray(jqobj, my_array);//-1 alert($("#deviceTypeRoot") == $("#deviceTypeRoot"));//False alert($("#deviceTypeRoot") === $("#deviceTypeRoot"));//False ...

Does it make sense to check for identity in __eq__?

When implementing a custom equality function for a class, does it make sense to check for identity first? An example: def __eq__(self, other): return (self is other) or (other criteria) This interesting is for cases when the other criteria may be more expensive (e.g. comparing some long strings). ...

Type test in F# on sequences, general equality test

Hello, I am trying to test equality of two elements. Why do I get the error: "does not have any proper subtypes and cannot be used as the source of a type test or runtime coercion", in the first pattern match: let eq a b = match (a,b) with | :? (seq<_>*seq<_>) -> Seq.map2( fun xA xB -> xA=xB ) a b ...

Why doesn't == and != defaults to object.Equals for ValueTypes?

For classes, == and != uses object.ReferenceEquals. But for structs, == and != is not defined. struct S { } S s1 = new S(); s1 is ValueType; // true S s2 = new S(); object.Equals(s1, s2); // true s1 == s2; // operator '==' cannot be applied. The default behavior for ValueType equals is reflecting over all fields and checking equality,...

Strange behaviour of the Array type

scala> List(1,2,3) == List(1,2,3) res2: Boolean = true scala> Map(1 -> "Olle") == Map(1 -> "Olle") res3: Boolean = true But when trying to do the same with Array, it does not work the same. Why? scala> Array('a','b') == Array('a','b') res4: Boolean = false I have used 2.8.0.RC7 and 2.8.0.Beta1-prerelease. ...

Highlight when equality operator (==) is used for string comparisons in Eclipse

Is there any way I can get Eclipse to highlight the use of the == operator to test String equality? I keep mistakenly using it instead of calling .equals(). I'd really like to make that into a warning and require an @SuppressWarnings annotation to remove it, in the yet-to-happen case that I actually want to compare strings for object e...

Testing for floating-point value equality: Is there a standard name for the "precision" constant?

I just read this nice answer given on how to compare floating-point values for equality. The following (slightly modified by me) is suggested instead of straight-forward comparison to 0: const double epsilon = 1e-5; double d = ...; if (Math.Abs(d) < epsilon) { // d is considered equal to 0. } My question is about the name of the ...

Equality comparison between multiple variables

I've a situation where I need to check whether multiple variables are having same data such as var x=1; var y=1; var z=1; I want to check whether x==1 and y==1 z==1 (it may be '1' or some other value). instead of this, is there any short way I can achieve same such as below if(x==y==z==1) Is this possible in C#? ...

bash string equality

In bash, what's the difference, if any, between the equal and double equal test operators? [[ "a" = "a" ]] && echo equal || echo not-equal [[ "a" == "a" ]] && echo equal || echo not-equal [[ "a" = "b" ]] && echo equal || echo not-equal [[ "a" == "b" ]] && echo equal || echo not-equal results in: equal equal not-equal not-equal ...

NHibernate - does NHibernate recognise object equality and change is save behaviour accordingly?

Hi all Let's say you have two classes, Person and Address. Person has a reference to Address like this: public class Person { public virtual Address Residence {get;set;} } Address has an override of .Equals which determines whether two Address instances represent the same physical address (by comparing postcode and first line, say...

Really awkward (seemingly simple) bug with python integer comparisons.

I have the following piece of code which is not working the way I expect it to at all... current_frame = 15 # just for showcasing purposes g_ch = 7 if (current_frame != int(row[0])) and (int(row[1]) != g_ch): current_frame = int(row[0]) print "curious=================================" pri...

elegant way to test python ASTs for equality (not reference or object identity)

Not sure of the terminology here, but this would be difference between eq? and equal? in scheme, or the difference between == and strncmp with C strings; where in each case the first would return false for two different strings that actually have the same content and the second would return true. I'm looking for the latter operation, f...

Scala, Java and equality

val filesHere = (new java.io.File(".")).listFiles val filesHere2 = (new java.io.File(".")).listFiles scala> filesHere == filesHere2 res0: Boolean = false That is quite counter intuitive. I would rather expect that filesHere and filesHere2 are equal. This is certainly due to a semantics mismatch between Java and Scala, e.g., ab...