equality

If operator< works properly for floating-point types, why can't we use it for equality testing?

时间秒杀一切 提交于 2019-11-30 11:16:25
Properly testing two floating-point numbers for equality is something that a lot of people, including me, don't fully understand. Today, however, I thought about how some standard containers define equality in terms of operator< . I always see people with problems surrounding equality, but never with the other relational comparisons. There are even silent versions of them to use, which include everything except for equality and inequality. Assuming operator< works "properly", unlike operator== , why couldn't we do this: bool floateq(float a, float b) { //check NaN return !(a < b) && !(b < a);

When would JavaScript == make more sense than ===?

落花浮王杯 提交于 2019-11-30 11:14:55
As Which equals operator (== vs ===) should be used in JavaScript comparisons? indicates they are basically identical except ' === ' also ensures type equality and hence ' == ' might perform type conversion. In Douglas Crockford's JavaScript: The Good Parts , it is advised to always avoid ' == '. However, I'm wondering what the original thought of designing two set of equality operators was. Have you seen any situation that using ' == ' actually is more suitable than using ' === '? Consider a situation when you compare numbers or strings: if (4 === 4) { // true } but if (4 == "4") { // true }

When are two enums equal in C#?

狂风中的少年 提交于 2019-11-30 09:25:33
I have created two enums and I know they are not the same but still I think it makes sense they would be equal since their string representation as well as their numeral representation are equal (and even the same...). In other words : I would like the first test to pass and the second one to fail. In reality however, they both fail. So : when are two enums in C# equal? Or is there anyway to define the equals operator in C#? Thanks! public enum enumA {one, two} public enum enumB {one, two} [Test] public void PreTest() { Assert.AreEqual(enumA.one,enumB.one); Assert.AreSame(enumA.one, enumB.one)

== vs Equals in C#

半世苍凉 提交于 2019-11-30 08:45:28
What is the difference between the evaluation of == and Equals in C#? For Ex, if(x==x++)//Always returns true but if(x.Equals(x++))//Always returns false Edited: int x=0; int y=0; if(x.Equals(y++))// Returns True According to the specification, this is expected behavior. The behavior of the first is governed by section 7.3 of the spec: Operands in an expression are evaluated from left to right. For example, in F(i) + G(i++) * H(i) , method F is called using the old value of i, then method G is called with the old value of i, and, finally, method H is called with the new value of i. This is

Is it important to override Equals if I'm implementing IEquatable<T>?

只愿长相守 提交于 2019-11-30 08:37:28
问题 I know the importance of overriding GetHashCode when implementing custom equality checks - for which I have implemented IEquality<T> interface, and also the difference between generic and non-generic Equals as discussed here. Now is there a point to override Equals(object t) ? Wouldn't everything come under generic Equals(T t) ? public override int GetHashCode() //required for hashsets and dictionaries { return Id; } public bool Equals(T other) //IEquatable<T> here { return Id == other.Id; }

Groovy GStringImpl and String behaviour

杀马特。学长 韩版系。学妹 提交于 2019-11-30 08:26:39
I've recently been reading about the behavior of GStringImpl s vs String s when used in collections in Groovy. I understand that the reason this evaluates to false... "${'test'}".equals("test") == false is due to the symmetry requirement of the .equals() contract, however I was wondering if there was a reason the GStringImpl couldn't just be evaluated to a String immediately. So when I do something like this... "${'someString'}" I don't get a GStringImpl , I just get a plain Java String back, which I can immediately use as the key in a map, for example. I know there are some workarounds, like

Using scanf in a while loop

荒凉一梦 提交于 2019-11-30 08:20:50
问题 Probably an extremely simple answer to this extremely simple question: I'm reading "C Primer Plus" by Pratta and he keeps using the example while (scanf("%d", &num) == 1)... Is the == 1 really necessary? It seems like one could just write: while (scanf("%d", &num)) It seems like the equality test is unnecessary since scanf returns the number of objects read and 1 would make the while loop true. Is the reason to make sure that the number of elements read is exactly 1 or is this totally

is StringComparison.Ordinal the same as InvariantCulture for testing equality?

倖福魔咒の 提交于 2019-11-30 08:13:34
From their brief summary descriptions, it sounds like the string comparison rules StringComparison.Ordinal and StringComparison.InvariantCulture are meant to differ in how they do sorting of strings. Is that all ? i.e., does that mean we can use either string comparison rule when doing an equality comparison? string.Equals(a, b, StringComparison....) And for extra credit: does it make a difference to the answer if we compare OrdinalIgnoreCase and InvariantCultureIgnoreCase ? How? Please provide supporting argument and/or references. It does matter, for example - there is a thing called

Comparing primitive to wrapper object with == behaviour unexplained

懵懂的女人 提交于 2019-11-30 07:59:43
I have a piece of code which I need to understand: public static void main(String[] args) { Character c = new Character('a'); Character cy = new Character('a'); char cx = 'a'; System.out.println(c == cx); System.out.println(cx == cy); System.out.println(c == cy); } Output: true true false I am unable to understand why only the third statement is failing. EDIT: This question is different to the .equals vs == question as this about primitive versus object comparison. c and cy refer to different instances of the Character class (each time you invoke a constructor, you create a new instance), so

Is string interning done at compile time in Java? [duplicate]

风流意气都作罢 提交于 2019-11-30 06:43:23
This question already has an answer here: When are Java Strings interned? 2 answers I am really confused with how string interning works in Java. When I write: String a = "ABC"; String b = "ABC"; if (a==b) System.out.println("Equal"); Does the compiler store the string literal "ABC" into the string constant pool at compile time? That sounds illogical, because I thought the string constant pool was created by the JVM at runtime, and I don't see how that is possible if it is done at compile time since the Java compiler does not even invoke the JVM. If it is not done at compile time and it is