comparison-operators

Is the operation “false < true” well defined?

泄露秘密 提交于 2019-11-29 23:24:42
Does the C++ specification define: the existence of the 'less than' operator for boolean parameters, and if so, the result of the 4 parameter permutations? In other words, are the results from the following operations defined by the specification? false < false false < true true < false true < true On my setup (Centos 7, gcc 4.8.2) , the code below spits out what I'd expect (given C's history of representing false as 0 and true as 1): false < false = false false < true = true true < false = false true < true = false Whilst I'm pretty sure most (all?) compilers will give the same output, is

Where in the python docs does it allow the `in` operator to be chained?

懵懂的女人 提交于 2019-11-29 13:53:30
I recently discovered that the following returns True : 'a' in 'ab' in 'abc' I'm aware of the python comparison chaining such as a < b < c , but I can't see anything in the docs about this being legal. Is this an accidental feature in the implementation of CPython, or is this behaviour specified? This is fully specified behaviour, not an accidental feature. Operator chaining is defined in the Comparison operators section : Comparisons can be chained arbitrarily, e.g., x < y <= z is equivalent to x < y and y <= z , except that y is evaluated only once (but in both cases z is not evaluated at

Why [] == [] is false in JavaScript?

泄露秘密 提交于 2019-11-29 06:56:30
I am working on a part of the code where I have an array which looks like [[data]] . The data is rendered on the server side through the Django template engine. So my code looks like this: var data = {{ series|safe }}; // data will be [[]] if no data is present if (data ==[[]]) console.log('no data'); The if always returns false . That means in [[]] == [[]] is false and my test shows that []==[] is false as well. Any descriptions would be appreciated. Because == (and === ) test to see if two objects are the same object and not if they are identical objects. Most test frameworks will include

Why does new String('hello') === new String('hello') evaluate to False? [duplicate]

喜欢而已 提交于 2019-11-29 02:04:53
问题 This question already has an answer here: Which equals operator (== vs ===) should be used in JavaScript comparisons? 49 answers JavaScript comparison operators: Identity vs. Equality 4 answers Why does the following statement return false in JavaScript? new String('hello') === new String('hello') 回答1: Two String objects will always be unequal to each other. Note that JavaScript has string primitive values as well as a String constructor to create wrapper objects. All object equality

Is comparison of const_iterator with iterator well-defined?

让人想犯罪 __ 提交于 2019-11-29 01:02:49
Consider the following code: #include <vector> #include <iostream> int main() { std::vector<int> vec{1,2,3,5}; for(auto it=vec.cbegin();it!=vec.cend();++it) { std::cout << *it; // A typo: end instead of cend if(next(it)!=vec.end()) std::cout << ","; } std::cout << "\n"; } Here I've introduced a typo: in the comparison I called vec.end() instead of vec.cend() . This appears to work as intended with gcc 5.2. But is it actually well-defined according to the Standard? Can iterator and const_iterator be safely compared? Surprisingly, C++98 and C++11 didn't say that you can compare a iterator with a

Is the operation “false < true” well defined?

半城伤御伤魂 提交于 2019-11-28 20:41:34
问题 Does the C++ specification define: the existence of the 'less than' operator for boolean parameters, and if so, the result of the 4 parameter permutations? In other words, are the results from the following operations defined by the specification? false < false false < true true < false true < true On my setup (Centos 7, gcc 4.8.2) , the code below spits out what I'd expect (given C's history of representing false as 0 and true as 1): false < false = false false < true = true true < false =

Why is === faster than == in PHP?

╄→гoц情女王★ 提交于 2019-11-28 15:14:00
Why is === faster than == in PHP? Because the equality operator == coerces, or converts the data type temporarily to see if it's equal to the other operand whereas === ( identity operator ) doesn't need to do any converting whatsoever and thus less work is done, making it faster. === does not perform typecasting, so 0 == '0' evaluates to true , but 0 === '0' - to false . First, === checks to see if the two arguments are the same type - so the number 1 and the string '1' fails on the type check before any comparisons are actually carried out. On the other hand, == doesn't check the type first

How do chained comparisons in Python actually work?

我只是一个虾纸丫 提交于 2019-11-28 11:16:28
The Python Doc for Comparisons says: Comparisons can be chained arbitrarily, e.g., x < y <= z is equivalent to x < y and y <= z , except that y is evaluated only once (but in both cases z is not evaluated at all when x < y is found to be false). And these SO questions/answers shed some more light on such usage: Python comparison operators chaining/grouping left to right? What does "evaluated only once" mean for chained comparisons in Python? , in particular the currently-accepted answer So something like (contrived example): if 1 < input("Value:") < 10: print "Is greater than 1 and less than

Sympy - Comparing expressions

五迷三道 提交于 2019-11-28 10:50:11
Is there a way to check if two expressions are mathematically equal? I expected tg(x)cos(x) == sin(x) to output True , but it outputs False . Is there a way to make such comparisons with sympy? Another example is (a+b)**2 == a**2 + 2*a*b + b**2 which surprisingly also outputs False . I found some similar questions, but none covered this exact problem. From the SymPy documentation == represents exact structural equality testing. “Exact” here means that two expressions will compare equal with == only if they are exactly equal structurally. Here, (x+1)^2 and x^2+2x+1 are not the same symbolically

Where in the python docs does it allow the `in` operator to be chained?

六眼飞鱼酱① 提交于 2019-11-28 08:03:15
问题 I recently discovered that the following returns True : 'a' in 'ab' in 'abc' I'm aware of the python comparison chaining such as a < b < c , but I can't see anything in the docs about this being legal. Is this an accidental feature in the implementation of CPython, or is this behaviour specified? 回答1: This is fully specified behaviour, not an accidental feature. Operator chaining is defined in the Comparison operators section: Comparisons can be chained arbitrarily, e.g., x < y <= z is