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
1
vote
2 answers

Can a function called in an equality comparison know what it's being compared against?

Is it possible to get string/int/function/or anything to compare in php function, if its used in if statement? function return_str($str) { $value = valueToCompare(); // I want to get "cat" here if($str == $value) { return $value; …
aksu
  • 5,221
  • 5
  • 24
  • 39
1
vote
2 answers

What are the criteria for using a new memory location when initializing a variable?

I'm running into some issues when writing a new unit test. My comments in the code example explain my specific issue: KeyboardLayout layout = Engine.ParseLayout(Dimensions.Create(300, 300), "FooBar", provider); var dweeb =…
lonious
  • 676
  • 9
  • 25
1
vote
3 answers

Why does Assert.AreEqual fail only in one direction with my custom type that has a widening conversion?

I'm creating a wrapped type similar to Nullable(Of T) and I'm writing some unit test to test equality. Like Nullable(Of T) I have implicit conversion between MyWrapperType(Of T) and T (both directions). Therefore, I would have expected all of the…
Jeff B
  • 8,572
  • 17
  • 61
  • 140
1
vote
1 answer

Is there an opposite of 'eq' in Lisp?

I am comparing the first value of two lists, with two outcomes, they are either equal or unequal. My first IF statement is: (if (eq (car L1) (car L2))) Is there an opposite of 'eq' that can I use? Like... (if (not eq (car L1) (car L2))) Any help…
hunterge
  • 657
  • 2
  • 10
  • 15
1
vote
3 answers

Optimizing the equality and inequality operators

I have some structures that are very expensive to compare. (They are actually trees with distinct branches.) Computing hash values for them is also expensive. I want to create a decorator for the eq operator that will cache some results to speed…
haael
  • 972
  • 2
  • 10
  • 22
1
vote
2 answers

Comparing two data structures for similarties

I'm trying to find an algorithm for checking similarities between two data entries. Say I have two data structures (fields in contact's list) with following data: // UserA addressbook. name: Frank Sinatra mobile: +44 555 555 555 55 // UserB…
David Sergey
  • 364
  • 1
  • 3
  • 18
1
vote
1 answer

C# Comparing complex objects returning list of differences

I've been working on a project for a while to parse a list of entries from a csv file and use that data to update a database. For each entry I create a new user instance that I put in a collection. Now I want to iterate that collection and compare…
Leegaert
  • 21
  • 4
1
vote
7 answers

Java Database Programming - Why aren't these values "equal"?

I am working on a project for school which requires a very basic log in function (nothing too fancy, seeing as how the passwords will remain in plain text). I have a test database and have thoroughly tested that all values that are being referenced…
draztick
  • 31
  • 3
1
vote
3 answers

Two distinct objects are being treated the same when used as keys in an object

I have an object where the keys are WebSockets (from the Node.JS ws library). Say I have two different WebSockets ("Socket A" and "Socket B"). socketa === socketb // => false Socket A is the only key in the object: theObject.hasOwnProperty(socketa)…
JJJollyjim
  • 5,837
  • 19
  • 56
  • 78
1
vote
2 answers

Why in scala a comparison between integer and floating point such as 71 == 71.0 is true?

I got this in the scala interpreter: scala> val a:Float = 71F; val b:Int = 71; if (a==b) println ("?") ? a: Float = 71.0 b: Int = 71 And I was wondering what are the exact semantics of this comparison. Even though I have a superficial knowledge of…
DPM
  • 1,960
  • 3
  • 26
  • 49
1
vote
1 answer

What are the roles of IEqualityComparer and IEquatable in the Enumerable.SequenceEqual method

On this page on MSDN they describe the SequenceEqual method of the Enumerable class. Halfway down the page it states: If you want to compare the actual data of the objects in the sequences instead of just comparing their references, you have to…
Mishax
  • 4,442
  • 5
  • 39
  • 63
1
vote
3 answers

Perl: Iterating over class member variables in overloaded equality sub

I'm trying to override the equality (==) operator for a class I created but I'm now facing a problem and I don't see a way out. Any help would be greatly appreciated. Here's the new() sub for the class: sub new { my $invocant = shift; my…
1
vote
2 answers

PHPUnit very odd results

assertEquals() fails when comparing '0' to false, but passes when comparing '1' to true $this->assertEquals( '0', false ); // fails $this->assertEquals( '1', true ); // passes Can someone explain this?
Joe Z
  • 306
  • 1
  • 3
  • 12
1
vote
1 answer

JavaScript: Object vs True (or other Primatives) Equality Comparison (Performance/Optimization)

So from a basic understanding of JavaScript === is faster than == (in most cases)(because === does not have to perform type-casting). Recently I saw someone setup a high-scoped set of variables for the entire function/object/class: var val = {}; //…
abaines
  • 224
  • 1
  • 5
  • 11
1
vote
5 answers

Equal but not interchangeable

I have two objects that are something like this: class Container { public HashSet Items { get; } } class Item { public Container Parent { get; set; } public string Value1 { get; set; } public int Value2 { get; set; } } Every…
HappyNomad
  • 4,458
  • 4
  • 36
  • 55