equality

Comparing NaN values for equality in Javascript

余生长醉 提交于 2019-11-27 10:40:29
问题 I need to compare two numeric values for equality in Javascript. The values may be NaN as well. I've come up with this code: if (val1 == val2 || isNaN(val1) && isNaN(val2)) ... which is working fine, but it looks bloated to me. I would like to make it more concise. Any ideas? 回答1: Try using Object.is() , it determines whether two values are the same value. Two values are the same if one of the following holds: both undefined both null both true or both false both strings of the same length

When to use == and when to use is?

◇◆丶佛笑我妖孽 提交于 2019-11-27 08:52:27
Curiously: >>> a = 123 >>> b = 123 >>> a is b True >>> a = 123. >>> b = 123. >>> a is b False Seems a is b being more or less defined as id(a) == id(b) . It is easy to make bugs this way: basename, ext = os.path.splitext(fname) if ext is '.mp3': # do something else: # do something else Some fnames unexpectedly ended up in the else block. The fix is simple, we should use ext == '.mp3' instead, but nonetheless if ext is '.mp3' on the surface seems like a nice pythonic way to write this and it's more readable than the "correct" way. Since strings are immutable, what are the technical details of

Equality comparison between multiple variables

早过忘川 提交于 2019-11-27 08:33:59
I've a situation where I need to check whether multiple variables are having same data such as var x=1; var y=1; var z=1; I want to check whether x==1 and y==1 z==1 (it may be '1' or some other value). instead of this, is there any short way I can achieve same such as below if(x==y==z==1) Is this possible in C#? KennyTM is correct, there is no other simpler or more efficient way. However, if you have many variables, you could also build an array of the values and use the IEnumerable.All method to verify they're all 1. More readable, IMO. if (new[] { v1, v2, v3, v4, v5, v6, v7, v8, v9, v10 }

Comparing two List<string> for equality

依然范特西╮ 提交于 2019-11-27 08:10:32
Other than stepping through the elements one by one, how do I compare two lists of strings for equality (in .NET 3.0): This fails: // Expected result. List<string> expected = new List<string>(); expected.Add( "a" ); expected.Add( "b" ); expected.Add( "c" ); // Actual result actual = new List<string>(); actual.Add( "a" ); actual.Add( "b" ); actual.Add( "c" ); // Verdict Assert.IsTrue( actual == expected ); dahlbyk Many test frameworks offer a CollectionAssert class: CollectionAssert.AreEqual(expected, actual); E.g MS Test JaredPar Try the following var equal = expected.SequenceEqual(actual);

Comparing arrays for equality in C++

北城以北 提交于 2019-11-27 07:45:41
Can someone please explain to me why the output from the following code is saying that arrays are not equal ? int main() { int iar1[] = {1,2,3,4,5}; int iar2[] = {1,2,3,4,5}; if (iar1 == iar2) cout << "Arrays are equal."; else cout << "Arrays are not equal."; return 0; } if (iar1 == iar2) Here iar1 and iar2 are decaying to pointers to the first elements of the respective arrays. Since they are two distinct arrays, the pointer values are, of course, different and your comparison tests not equal. To do an element-wise comparison, you must either write a loop; or use std::array instead std::array

Comparing two dictionaries with numpy matrices as values

瘦欲@ 提交于 2019-11-27 07:39:26
问题 I want to assert that two Python dictionaries are equal (that means: equal amount of keys, and each mapping from key to value is equal; order is not important). A simple way would be assert A==B , however, this does not work if the values of the dictionaries are numpy arrays . How can I write a function to check in general if two dictionaries are equal? >>> import numpy as np >>> A = {1: np.identity(5)} >>> B = {1: np.identity(5) + np.ones([5,5])} >>> A == B ValueError: The truth value of an

Why does 1234 == '1234 test' evaluate to true? [duplicate]

只愿长相守 提交于 2019-11-27 07:02:13
Possible Duplicate: php == vs === operator An easy answer for someone I'm sure. Can someone explain why this expression evaluates to true? (1234 == '1234 test') Lucas Green Because you are using the == (similarity) operator and PHP is coercing the string to an int. To resolve it use the === (equality) operator, which checks not only if the value is the same, but also if the data type is the same, so "123" string and 123 int won't be considered equal. In PHP (and JavaScript -- which has slightly different behavior), the comparison operator == works differently than it does in strongly-typed

What happens when you call `if key in dict`

只谈情不闲聊 提交于 2019-11-27 06:56:54
问题 I have a class (let's call it myClass ) that implements both __hash__ and __eq__ . I also have a dict that maps myClass objects to some value, computing which takes some time. Over the course of my program, many (in the order of millions) myClass objects are instantiated. This is why I use the dict to keep track of those values. However, sometimes a new myClass object might be equivalent to an older one (as defined by the __eq__ method). So rather than compute the value for that object again,

Equality in Kotlin

 ̄綄美尐妖づ 提交于 2019-11-27 06:52:23
问题 I'm learning Kotlin, with a C++ and Java background. I was expecting the following to print true , not false . I know that == maps to equals . Does the default implementation of equals not compare each member, i.e. firstName and lastName ? If so, wouldn't it see the string values as equal (since == maps to equal again)? Apparently there's something related to equality versus identity that I haven't got right in Kotlin yet. class MyPerson(val firstName: String, val lastName: String) fun main

Compare two objects in Java with possible null values

て烟熏妆下的殇ゞ 提交于 2019-11-27 06:21:24
I want to compare two strings for equality in Java, when either or both could be null , so I cannot simply call .equals() . What is the best way? boolean compare(String str1, String str2) { ... } Edit: return ((str1 == str2) || (str1 != null && str1.equals(str2))); This is what Java internal code uses (on other compare methods): public static boolean compare(String str1, String str2) { return (str1 == null ? str2 == null : str1.equals(str2)); } Since Java 7 you can use the static method java.util.Objects.equals(Object, Object) to perform equals checks on two objects without caring about them