equality

checking for equality between an int and float in C

徘徊边缘 提交于 2019-12-01 22:29:05
I came across this piece of code : int x=3; float y=3.0; if(x==y) printf("x and y are equal"); else printf("x and y are not equal"); Why does this code print "x and y are equal"?? Here if y=3.1(say), then the code prints "x and y are not equal". Someone please explain how is this happening. Comparisons between arithmetic types are subject to the so-called usual arithmetic conversions (§5/9, §5.9/2, §5.10/1). Emphasis mine. Many binary operators that expect operands of arithmetic or enumeration type cause conversions and yield result types in a similar way. The purpose is to yield a common type

Why does +[UIColor whiteColor] not equal another white?

断了今生、忘了曾经 提交于 2019-12-01 22:08:12
This is something really strange. Comparing +[UIColor redColor] with a red I create myself gives an equal result, but comparing +[UIColor whiteColor] to another white does not. // This test passes. XCTAssertEqualObjects([UIColor redColor], [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0], @"Red should equal red."); // While this test fails! XCTAssertEqualObjects([UIColor whiteColor], [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0], @"White should equal white."); While I'm extending UIColor with some useful additions, this fact can be really annoying. Can somebody shed a light on

Floating-point equality test and extra precision: can this code fail?

折月煮酒 提交于 2019-12-01 21:08:34
The discussion started under my answer to another question . The following code determines machine epsilon : float compute_eps() { float eps = 1.0f; while (1.0f + eps != 1.0f) eps /= 2.0f; return eps; } In the comments it was proposed that the 1.0f + eps != 1.0f test might fail because C++ standard permits the use of extra precision. Although I'm aware that floating-point operations are actually performed in higher precision (than specified by the actual types used), I happen to disagree with this proposal. I doubt that during the comparison operations, such as == or != , the operands are not

Why does == not work while comparing two object type variables boxed with same int value

浪尽此生 提交于 2019-12-01 17:31:21
While trying to implement a simple singly linked list in C#, I noticed that == does not work while comparing two object type variables boxed with an int value but .Equals works. Wanted to check why that is so. The below snippet is a generic object type Data property public class Node { /// <summary> /// Data contained in the node /// </summary> private object Data { get; set; }; } The below code traverses the singly linked list and searches for a value of type object - /// <summary> /// <param name="d">Data to be searched in all the nodes of a singly linked list /// Traverses through each node

Why does == not work while comparing two object type variables boxed with same int value

感情迁移 提交于 2019-12-01 17:05:10
问题 While trying to implement a simple singly linked list in C#, I noticed that == does not work while comparing two object type variables boxed with an int value but .Equals works. Wanted to check why that is so. The below snippet is a generic object type Data property public class Node { /// <summary> /// Data contained in the node /// </summary> private object Data { get; set; }; } The below code traverses the singly linked list and searches for a value of type object - /// <summary> ///

Comparing structs for equality without boxing

大城市里の小女人 提交于 2019-12-01 16:46:41
I came across an extension method that applies to structs (SomeStruct) and returns whether or not the value is equal to the default(SomeStruct) (when the parameterless constructor is called). public static bool IsDefault<T> (this T value) where T : struct { return (!EqualityComparer<T>.Default.Equals(value, default(T))); } This got me wondering whether the struct was being boxed. This is purely out of curiosity as there are pros/cons to boxing/passing by value depending on the context. Assumptions: The first of the following methods is illegal since structs do not implicitly override the

What is the difference between != and <>? [duplicate]

不想你离开。 提交于 2019-12-01 16:19:44
This question already has an answer here: Python not equal operator 4 answers Perhaps this is a rather newbie-ish question, but I'm curious. I have tried searching for it, but I suppose I lack the correct terminology to search properly. Difference between != and <> . On searching again, "inequality", I found one that discusses not == and != , but nothing about <> . In Python 2.x, <> is equivalent to != , as described in the documentation : The forms <> and != are equivalent; for consistency with C, != is preferred; where != is mentioned below <> is also accepted. The <> spelling is considered

Why a generic type definition implemented interfaces lose type information?

╄→гoц情女王★ 提交于 2019-12-01 15:57:59
For example, if you run the following code... Type IListType = new List<string>().GetType() .GetInterface("IList`1") .GetGenericTypeDefinition(); ...and you watch IListType variable, you'll find that the whole Type instance has all properties available like FullName and others. But what happens when you run the code bellow? Type IListType2 = typeof(List<>).GetInterface("IList`1") Now IListType got from a generic type definition isn't the same as the first code sample: most Type properties will return null. The main issue with this is that IListType == IListType2 doesn't equal while they're the

.NET Dictionaries have same keys and values, but aren't “equal”

隐身守侯 提交于 2019-12-01 15:45:53
This test fails: using Microsoft.VisualStudio.TestTools.UnitTesting; [TestMethod()] public void dictEqualTest() { IDictionary<string, int> dict = new Dictionary<string, int>(); IDictionary<string, int> dictClone = new Dictionary<string, int>(); for (int x = 0; x < 3; x++) { dict[x.ToString()] = x; dictClone[x.ToString()] = x; } Assert.AreEqual(dict, dictClone); // fails here Assert.IsTrue(dict.Equals(dictClone)); // and here, if the first is commented out Assert.AreSame(dict, dictClone); // also fails } Am I misunderstanding something about how a Dictionary works? I'm looking for the Java

What is the difference between != and <>? [duplicate]

眉间皱痕 提交于 2019-12-01 15:16:25
问题 This question already has answers here : Python not equal operator (4 answers) Closed 5 years ago . Perhaps this is a rather newbie-ish question, but I'm curious. I have tried searching for it, but I suppose I lack the correct terminology to search properly. Difference between != and <> . On searching again, "inequality", I found one that discusses not == and != , but nothing about <> . 回答1: In Python 2.x, <> is equivalent to != , as described in the documentation: The forms <> and != are