equality

Is “(float)integer == integer” guaranteed to be equal in C#?

青春壹個敷衍的年華 提交于 2019-12-01 02:21:28
While "we all know" that x == y can be problematic, where x and y are floating point values, this question is a bit more specific: int x = random.Next(SOME_UPPER_LIMIT); float r = x; // Is the following ALWAYS true? r == x Now, since the range of float of is much larger than that of integers (but the precision is insufficient to uniquely present integers at the edges), it would be nice if responses to this question also addressed which values of x the above can be guaranteed for, if it can be guaranteed at all. Currently my code is making this assumption (for relatively small values of x) - I

Comparing two string arrays in C#

你离开我真会死。 提交于 2019-12-01 02:02:56
Say we have 5 string arrays as such: string[] a = {"The","Big", "Ant"}; string[] b = {"Big","Ant","Ran"}; string[] c = {"The","Big","Ant"}; string[] d = {"No","Ants","Here"}; string[] e = {"The", "Big", "Ant", "Ran", "Too", "Far"}; Is there a method to compare these strings to each other without looping through them in C# such that only a and c would yield the boolean true? In other words, all elements must be equal and the array must be the same size? Again, without using a loop if possible. You can use Linq: bool areEqual = a.SequenceEqual(b); Try using Enumerable.SequenceEqual : var equal =

Equality test of images using ImageMagick

久未见 提交于 2019-12-01 01:22:36
Is there any equality predicate function in ImageMagick library? I want to compare two images and find whether they are the exactly same (all colors of the pixels are the same) or have any differences. I’ve looked around, but it seems not to have a such function. Should I write the function using pixel iterators by myself? ImageMagick provides the compare function to properly compare images. Checking the md5 checksum of two images is not the correct approach, since some image formats (e.g. PNG and JPEG with EXIF for example), contain the date and time the file was created (see example 1) below

Result of calling IEquatable<T>.Equals(T obj) when this == null and obj == null?

自闭症网瘾萝莉.ら 提交于 2019-12-01 00:48:02
问题 What should IEquatable<T>.Equals(T obj) do when this == null and obj == null ? 1) This code is generated by F# compiler when implementing IEquatable<T> . You can see that it returns true when both objects are null : public sealed override bool Equals(T obj) { if (this == null) { return obj == null; } if (obj == null) { return false; } // Code when both this and obj are not null. } 2) Similar code can be found in the question "in IEquatable implementation is reference check necessary" or in

java: comparing classes with == or .equals(): is there a difference? [duplicate]

时光毁灭记忆、已成空白 提交于 2019-12-01 00:18:25
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Does Java guarantee that Object.getClass() == Object.getClass()? I know you're supposed to use equals() in general, but is there any way two Class<?> objects could be equal with equals() but not equal with == ? edit: I am specifically looking to find out whether two class objects exist such that Class<?> cl1 = ... Class<?> cl2 = ... cl1.equals(cl2) -> true cl1 == cl2 -> false This does not seemed to be covered

In C, does (x==y==z) behave as I'd expect?

僤鯓⒐⒋嵵緔 提交于 2019-12-01 00:01:00
问题 Can I compare three variables like the following, instead of doing if((x==y)&&(y==z)&&(z=x)) ? [The if statement should execute if all three variables have the same value. These are booleans.] if(debounceATnow == debounceATlast == debounceATlastlast) { debounceANew = debounceATnow; } else { debounceANew = debounceAOld; } 回答1: No, it does not. x == y is converted to int, yields 0 or 1 , and the result is compared to z . So x==y==z will yield true if and only if (x is equal to y and z is 1) or

How is “sameness” defined for Java objects?

巧了我就是萌 提交于 2019-11-30 23:28:34
I want to add objects of a custom type to a Set. I have several which are the same, i.e. they have the same values for their public variables. I don't want more than one instance of a "same" object to be added to the set, but each time a new object is created it is always added. This is because the equals method for class Object implements the most discriminating possible equivalence relation on objects: "For any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true)." Can I override the equals method for this

Comparing XmlDocument for equality (content wise)

北战南征 提交于 2019-11-30 22:29:48
问题 If I want to compare the contents of a XMlDocument, is it just like this? XmlDocument doc1 = GetDoc1(); XmlDocument doc2 = GetDoc2(); if(doc1 == doc2) { } I am not checking if they are both the same object reference, but if the CONTENTS of the xml are the same. 回答1: No. XmlDocument does not override the behavior of the Equals() method so, it is in fact just performing reference equality - which will fail in your example, unless the documents are actually the same object instance. If you want

Comparing two string arrays in C#

我的未来我决定 提交于 2019-11-30 22:20:19
问题 Say we have 5 string arrays as such: string[] a = {"The","Big", "Ant"}; string[] b = {"Big","Ant","Ran"}; string[] c = {"The","Big","Ant"}; string[] d = {"No","Ants","Here"}; string[] e = {"The", "Big", "Ant", "Ran", "Too", "Far"}; Is there a method to compare these strings to each other without looping through them in C# such that only a and c would yield the boolean true? In other words, all elements must be equal and the array must be the same size? Again, without using a loop if possible.

Relational operations using only increment, loop, assign, zero

萝らか妹 提交于 2019-11-30 20:38:55
This is a follow up question for: Subtraction operation using only increment, loop, assign, zero We're only allowed to use the following operations: incr(x) - Once this function is called it will assign x + 1 to x assign(x, y) - This function will assign the value of y to x (x = y) zero(x) - This function will assign 0 to x (x = 0) loop X { } - operations written within brackets will be executed X times For example, addition can be implemented as follows: add(x, y) { loop x { y = incr(y) } return y } How do I implement the relational operators using these four operations? The relational