equality

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

99封情书 提交于 2019-12-03 23:33:29
问题 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

How do the SQL “IS” and “=” operators differ?

北城余情 提交于 2019-12-03 23:03:46
I am building some prepared statements that use parametrized values. As an example: SELECT * FROM "Foo" WHERE "Bar"=@param Sometimes @param might be NULL . In such cases, I want the query to return records where Bar is NULL , but the above query will not do that. I have learned that I can use the IS operator for this. In other words: SELECT * FROM "Foo" WHERE "Bar" IS @param Aside from the differing treatment of NULL , are there any other ways in which the above two statements will behave differently? What if @param is not NULL , but is instead, let's say, 5 ? Is using the IS operator in that

Surprising Tuple (in)equality

霸气de小男生 提交于 2019-12-03 22:11:12
Until today, my understanding of .NET Tuple classes had been that they delegate their implementation of Equals() to their contents, allowing me to equate and compare them "by value". Then this test came along and made a fool out of me: [TestMethod] public void EquateTwoTuplesWithSameContent() { var t1 = Tuple.Create("S"); var t2 = Tuple.Create((object)t1.Item1); Assert.IsTrue(t1.Equals(t2)); // Boom! } Reading through MSDN documentation and various blogs has left me with more questions. From what I gather, it would seem that Tuple<object> and Tuple<TWhatever> are always considered not equal,

Cast to object before null check in overriding Equals [duplicate]

情到浓时终转凉″ 提交于 2019-12-03 20:52:03
问题 This question already has answers here : In the msdn guidance on Equals override, why the cast to object in the null check? (3 answers) Closed 2 years ago . Just reading the msdn article on overriding equality operators here The following snippet confuses me... // If parameter cannot be cast to Point return false. TwoDPoint p = obj as TwoDPoint; if ((System.Object)p == null) // <-- wtf? { return false; } Why is there a cast to Object here to perform the null comparison? 回答1: Operators apply

2 NSDates that should be equal aren't?

不羁的心 提交于 2019-12-03 20:10:15
I'm using the JSON library from Stig Brautaset(http://code.google.com/p/json-framework) and I need to serialize an NSDate. I was considering converting it into a string before JSONifying it, however, I ran into this weird behavior: Why aren't these NSDates considered equal? NSDate *d = [[NSDate alloc] init]; NSDate *dd = [NSDate dateWithString:[d description]]; NSLog(@"%@", d); NSLog(@"%@", dd); if( [d isEqualToDate:dd] ){ NSLog(@"Yay!"); } When you describe the original date object you lose some sub-second precision from the original object — in other words, -description shaves off fractional

Groovy GStringImpl and String behaviour

笑着哭i 提交于 2019-12-03 19:21:15
问题 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,

How do I check if two variables reference the same object in Python?

梦想的初衷 提交于 2019-12-03 19:09:24
问题 x and y are two variables. I can check if they're equal using x == y , but how can I check if they have the same identity? Example: x = [1, 2, 3] y = [1, 2, 3] Now x == y is True because x and y are equal, however, x and y aren't the same object. I'm looking for something like sameObject(x, y) which in that case is supposed to be False. 回答1: You can use is to check if two objects have the same identity. >>> x = [1, 2, 3] >>> y = [1, 2, 3] >>> x == y True >>> x is y False 回答2: To build on the

Why do these two comparisons have different results?

隐身守侯 提交于 2019-12-03 18:38:01
问题 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 回答1: Because new Byte() creates value type, which are compared by value (by default it will return byte with value 0 ). And new Byte[0] creates array, which is a reference type and compared by reference (and these two instances of array will have different references). See Value Types and Reference Types article for details. 回答2: Bytes are value

Checking equality of interface{}

一个人想着一个人 提交于 2019-12-03 15:14:21
问题 I am searching a []interface{} slice for a given interface{} value: var v interface{} for i := 0; i < len(A); i++ { if (A[i] == v) { fmt.Println("Gotcha!") break } } In the trivial case the types are int . However what should I do if, for example, the types are some custom struct ? 回答1: Thanks to @CodingPickle comment, I provide the following from the Go Programming Language Specification The equality operators == and != apply to operands that are comparable. Regarding interface{} s and

Tuple vs string as a Dictionary key in C#

妖精的绣舞 提交于 2019-12-03 14:41:03
问题 I have a cache that I implement using a ConcurrentDictionary, The data that I need to keep depends on 5 parameters. So the Method to get it from the cache is: (I show only 3 parameters here for simplicity, and I changed the data type to represent CarData for clearity) public CarData GetCarData(string carModel, string engineType, int year); I wonder what type of key will be better to use in my ConcurrentDictionary, I can do it like this: var carCache = new ConcurrentDictionary<string, CarData>