Questions tagged [equality]

Equality is a relationship between two or more items or variables or objects that exists if (1) the items are the same item, variable, or object or (2) the items are different items, variables or objects but they have the same value. This tag should generally be used with programming language specific tags as well as other contextual tags such as database system. The post should include as much context about the equality test as is possible.

This tag is typically used for questions concerning how to determine if two objects or variables are either the same object or different objects with the same value. Questions almost always require a programming language tag and other tags for context.

Programming languages represent variables, objects, or other storage containers using different underlying mechanisms for the storage of the values of variables and objects. A test for equality is to compare some characteristic of two variables or objects to determine if the characteristic is the same.

This characteristic is typically either (1) the storage identifier such as a reference handle, memory address, etc. when checking if the two are the same object or (2) the characteristic is the value of the objects retrieved using a storage identifier when checking if the value of the two are the same value.

Different programming languages and different operating environments have differences in how equality is tested and what operators are used to express a test for equality. In some cases various conversions from underlying storage representations may be involved in testing for equality.

See What is the difference between equality and equivalence? as well as Equivalence relation and natural ordering on a class in Java

2095 questions
86
votes
3 answers

What's the difference between eq, eql, equal and equalp, in Common Lisp?

What's the difference between eq, eql, equal and equalp, in Common Lisp? I understand that some of them check types, some of them check across types an all that, but which is which? When is one better to use than the others?
Cristián Romo
  • 9,814
  • 12
  • 50
  • 50
84
votes
8 answers

Testing for equality between dictionaries in C#

Assuming dictionary keys and values have their equals and hash methods implemented correctly, what is the most succinct and efficient way to test for equality of two dictionaries? In this context, two dictionaries are said to be equal if they…
rony l
  • 5,798
  • 5
  • 35
  • 56
80
votes
2 answers

Test for equality to the default value

The following doesn't compile: public void MyMethod(T value) { if (value == default(T)) { // do stuff } } Error: Operator '==' cannot be applied to operands of type 'T' and 'T' I can't use value == null because T may be a…
Greg
  • 23,155
  • 11
  • 57
  • 79
80
votes
6 answers

Why does 1234 == '1234 test' evaluate to true?

Possible Duplicate: php == vs === operator An easy answer for someone I'm sure. Can someone explain why this expression evaluates to true? (1234 == '1234 test')
Cjueden
  • 1,200
  • 2
  • 13
  • 25
77
votes
8 answers

Comparing two structs using ==

I am trying to compare two structs using equals (==) in C#. My struct is below: public struct CisSettings : IEquatable { public int Gain { get; private set; } public int Offset { get; private set; } public int Bright { get;…
JMK
  • 27,273
  • 52
  • 163
  • 280
74
votes
11 answers

Comparing NaN values for equality in Javascript

I need to compare two numeric values for equality in Javascript. The values may be NaN as well. I've come up with this code: if (val1 == val2 || isNaN(val1) && isNaN(val2)) ... which is working fine, but it looks bloated to me. I would like to make…
GOTO 0
  • 42,323
  • 22
  • 125
  • 158
73
votes
7 answers

Pandas DataFrames with NaNs equality comparison

In the context of unit testing some functions, I'm trying to establish the equality of 2 DataFrames using python pandas: ipdb> expect 1 2 2012-01-01 00:00:00+00:00 NaN 3 2013-05-14 12:00:00+00:00 3 NaN ipdb>…
Steve Pike
  • 1,454
  • 2
  • 12
  • 14
71
votes
5 answers

JavaScript Date Comparisons Don't Equal

I've tried searching for people with similar questions, but haven't found anything. I have two dates in JavaScript, both set to the same value... Equality Testing fails on ==, but >= and <= evaluate true. Below is the code I have in play: var…
Patrick
  • 735
  • 1
  • 5
  • 6
70
votes
6 answers

(.1f+.2f==.3f) != (.1f+.2f).Equals(.3f) Why?

My question is not about floating precision. It is about why Equals() is different from ==. I understand why .1f + .2f == .3f is false (while .1m + .2m == .3m is true). I get that == is reference and .Equals() is value comparison. (Edit: I know…
LZW
  • 1,035
  • 2
  • 10
  • 13
70
votes
1 answer

How to test two dateTimes for being the same date?

Possible Duplicate: How to compare Dates in C# This code of mine: public static string getLogFileNameForDate(DateTime dt) { if (dt.Equals(DateTime.Now)) ...fails even when the two dates are the same (date) because dt is assigned a value at…
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
69
votes
12 answers

Why does == equality comparison between arrays not work?

Can someone please explain to me why the output from the following code is saying that the arrays are not equal? int main() { int iar1[] = {1, 2, 3, 4, 5}; int iar2[] = {1, 2, 3, 4, 5}; if (iar1 == iar2) cout << "Arrays are…
vladinkoc
  • 919
  • 1
  • 8
  • 13
68
votes
4 answers

Why do these two comparisons have different results?

Why does this code return true: new Byte() == new Byte() // returns true but this code returns false: new Byte[0] == new Byte[0] // returns false
Maxim Zhukov
  • 10,060
  • 5
  • 44
  • 88
68
votes
6 answers

Can I use ' == ' to compare two vectors. I tried it and seems to be working fine. But I don't know whether it will work in more complex situations

First example: int main(){ using namespace std; vector v1{10, 20, 30, 40, 50}; vector v2{10, 20, 30, 40, 50}; if(v1==v2) cout<<"equal"; else cout<<"unequal"; } // it returns equal Second…
suman shadow
  • 793
  • 1
  • 5
  • 6
66
votes
1 answer

Testing anonymous function equality with Jest

Is there a way to test anonymous function equality with jest@20? I am trying to pass a test similar to: const foo = i => j => {return i*j} const bar = () => {baz:foo(2), boz:1} describe('Test anonymous function equality',()=>{ it('+++ foo', ()…
strider
  • 5,674
  • 4
  • 24
  • 29
66
votes
9 answers

Comparing two List for equality

Other than stepping through the elements one by one, how do I compare two lists of strings for equality (in .NET 3.0): This fails: // Expected result. List expected = new List(); expected.Add( "a" ); expected.Add( "b"…
Adam Kane
  • 3,996
  • 7
  • 44
  • 53