equality

Why do these two comparisons have different results?

空扰寡人 提交于 2019-11-29 23:26:42
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 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. Bytes are value types in .NET, meaning that the == operator returns true if and only if the two bytes have the same value. This is

Compare if two dataframe objects in R are equal?

杀马特。学长 韩版系。学妹 提交于 2019-11-29 22:41:41
How do I check if two objects, e.g. dataframes, are value equal in R? By value equal, I mean the value of each row of each column of one dataframe is equal to the value of the corresponding row and column in the second dataframe. It is not clear what it means to test if two data frames are "value equal" but to test if the values are the same, here is an example of two non-identical dataframes with equal values: a <- data.frame(x = 1:10) b <- data.frame(y = 1:10) To test if all values are equal: all(a == b) # TRUE To test if objects are identical (they are not, they have different column names)

Python if not == vs if !=

大憨熊 提交于 2019-11-29 18:45:26
What is the difference between these two lines of code: if not x == 'val': and if x != 'val': Is one more efficient than the other? Would it be better to use if x == 'val': pass else: jonrsharpe Using dis to look at the bytecode generated for the two versions: not == 4 0 LOAD_FAST 0 (foo) 3 LOAD_FAST 1 (bar) 6 COMPARE_OP 2 (==) 9 UNARY_NOT 10 RETURN_VALUE != 4 0 LOAD_FAST 0 (foo) 3 LOAD_FAST 1 (bar) 6 COMPARE_OP 3 (!=) 9 RETURN_VALUE The latter has fewer operations, and is therefore likely to be slightly more efficient. It was pointed out in the commments (thanks, @Quincunx ) that where you

Find whether two Swift Arrays contain the same elements

♀尐吖头ヾ 提交于 2019-11-29 17:25:45
I have two arrays let a1 = [obj1, obj2, obj3] let a2 = [obj3, obj2, obj1] Assume that the array elements are custom objects which are not sortable . I just want to check if the Array s contain the same elements, in any order. I tried this: if a1==a2 { print("S1 is the same as S2") } else { print("S1 is not the same as S2") } but I get "S1 is not the same as S2" as output. All I could think of are two solutions Sort and compare (doesn't work if elements are not sortable, say complex numbers) Subtract one array from the other Is there any built-in function or operation to check if they are equal

Strings don't seem to be equal in Java on Android, even though they print the same

两盒软妹~` 提交于 2019-11-29 17:16:05
问题 I've got a problem that I'm rather confused about. I have the following lines of code in my android application: System.out.println(CurrentNode.getNodeName().toString()); if (CurrentNode.getNodeName().toString() == "start") { System.out.println("Yes it does!"); } else { System.out.println("No it doesnt"); } When I look at the output of the first println statement it shows up in LogCat as "start" (without the quotes obviously). But then when the if statement executes it goes to the else

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

北慕城南 提交于 2019-11-29 16:47:35
问题 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=

In which case could “a != a” return “true”?

落爺英雄遲暮 提交于 2019-11-29 14:40:30
java.lang.Math#min(double, double) : public static double min(double a, double b) { if (a != a) return a; // a is NaN if (a == 0.0d && b == 0.0d && Double.doubleToLongBits(b) == negativeZeroDoubleBits) return b; return (a <= b) ? a : b; } In which case could a != a return true ? It seems that it's when a is NaN, but I can't imagine an example. Could you please provide one? A simple example is double d = Double.NaN; // or double d = 0.0/0.0; // or double d = Double.POSITIVE_INFINITY + Double.NEGATIVE_INFINITY; if (Double.isNaN(a)) { // tests if a != a // do something BTW Double.compare() does

Comparing two structs using ==

北城余情 提交于 2019-11-29 13:06:56
I am trying to compare two structs using equals (==) in C#. My struct is below: public struct CisSettings : IEquatable<CisSettings> { public int Gain { get; private set; } public int Offset { get; private set; } public int Bright { get; private set; } public int Contrast { get; private set; } public CisSettings(int gain, int offset, int bright, int contrast) : this() { Gain = gain; Offset = offset; Bright = bright; Contrast = contrast; } public bool Equals(CisSettings other) { return Equals(other, this); } public override bool Equals(object obj) { if (obj == null || GetType() != obj.GetType())

Why do comparisions between very large float values fail in python?

僤鯓⒐⒋嵵緔 提交于 2019-11-29 12:37:45
In my understanding, sys.float_info.max is the largest possible float value. However, it seems that comparing such large values fail . import math import sys m = sys.float_info.max # type 'float' m == m # True m < m # False m > m # False m == m-1.0 # True m < m-1.0 # False m > m-1.0 # False m == m-1e100 # True m < m-1e100 # False m > m-1e100 # False m == m-1e300 # False m > m-1e300 # True m < m-1e300 # False I assume that's because of the limited precision? If so, in what numerical range can i operate safely? The above code was run with Python 3.5.2. On a typical machine running Python, there

Comparing primitive to wrapper object with == behaviour unexplained

只谈情不闲聊 提交于 2019-11-29 10:53:45
问题 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. 回答1: c and cy refer to