Questions tagged [comparison-operators]

Comparison operators, as their name implies, allow to compare two values and usually return Boolean value (true or false).

658 questions
52
votes
2 answers

Why is operator!= removed in C++20 for many standard library types?

According to cppreference, std::type_info::operator!= gets removed with C++20, however, std::type_info::operator== apparently remains. What's the reasoning behind? I might agree on comparing for inequality being meaningless, but then comparing for…
49
votes
7 answers

Numeric comparison difficulty in R

I'm trying to compare two numbers in R as a part of a if-statement condition: (a-b) >= 0.5 In this particular instance, a = 0.58 and b = 0.08... and yet (a-b) >= 0.5 is false. I'm aware of the dangers of using == for exact number comparisons, and…
Matt Parker
  • 24,639
  • 6
  • 51
  • 71
49
votes
7 answers

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

Why does the following statement return false in JavaScript? new String('hello') === new String('hello')
49
votes
3 answers

Using comparison operators in SELECT clause of T-SQL query

How to select a result of comparison operator as a field with type BIT? How it does work in C#: bool isGreater = FieldA > FieldB; How it doesn't work in T-SQL: SELECT (FieldA > FieldB) AS BIT FROM t How to write such task properly?
abatishchev
  • 92,232
  • 78
  • 284
  • 421
45
votes
0 answers

What does `!!~` mean in javascript?

Possible Duplicate: What does tilde (~) preceding jQuery object do? I found a strange !!~ in the code when reading: https://github.com/LearnBoost/mongoose/blob/master/lib/document.js#L678 Document.prototype.isModified = function (path) { return…
Freewind
  • 177,284
  • 143
  • 381
  • 649
43
votes
3 answers

C# Type Comparison: Type.Equals vs operator ==

ReSharper suggests that the following be changed from: Type foo = typeof( Foo ); Type bar = typeof( Bar ); if( foo.Equals( bar ) ) { ... } To: if( foo == bar ) { ... } operator == // Summary: // Indicates whether two System.Type objects are…
Metro Smurf
  • 33,866
  • 20
  • 97
  • 127
43
votes
2 answers

What are the breaking changes caused by rewritten comparison operators?

There are some new rules about rewritten comparison operators in C++20, and I'm trying to understand how they work. I've run into the following program: struct B {}; struct A { bool operator==(B const&); // #1 }; bool operator==(B const&, A…
cigien
  • 50,328
  • 7
  • 37
  • 78
40
votes
3 answers

Why is 'True == not False' a syntax error in Python?

Comparing boolean values with == works in Python. But when I apply the boolean not operator, the result is a syntax error: Python 2.7 (r27:82500, Sep 16 2010, 18:02:00) [GCC 4.5.1 20100907 (Red Hat 4.5.1-3)] on linux2 Type "help", "copyright",…
Tim Martin
  • 3,486
  • 6
  • 30
  • 43
40
votes
11 answers

C# Nullable Equality Operations, Why does null <= null resolve as false?

Why is it that in .NET null >= null resolves as false, but null == null resolves as true? In other words, why isn't null >= null equivalent to null > null || null == null? Does anyone have the official answer?
Ben
  • 403
  • 1
  • 4
  • 4
39
votes
3 answers

JavaScript equality transitivity is weird

I've been reading Douglas Crockford's JavaScript: The Good Parts, and I came across this weird example that doesn't make sense to me: '' == '0' // false 0 == '' // true 0 == '0' // true false == undefined //…
Hristo
  • 42,002
  • 60
  • 155
  • 224
37
votes
3 answers

Is JavaScript's double equals (==) always symmetric?

There are many cases in which JavaScript's type-coercing equality operator is not transitive. For example, see "JavaScript equality transitivity is weird." However, are there any cases in which == isn't symmetric? That is, where a == b is true and b…
Trevor Burnham
  • 74,631
  • 30
  • 153
  • 193
32
votes
4 answers

"is not required" == undefined behavior?

My question is mainly about terminology and how to interpret the standard. [expr.rel]#4: The result of comparing unequal pointers to objects is defined in terms of a partial order consistent with the following rules: (4.1) If two pointers point to…
32
votes
3 answers

Is comparison of const_iterator with iterator well-defined?

Consider the following code: #include #include int main() { std::vector vec{1,2,3,5}; for(auto it=vec.cbegin();it!=vec.cend();++it) { std::cout << *it; // A typo: end instead of cend …
Ruslan
  • 15,183
  • 5
  • 55
  • 110
32
votes
1 answer

Python's in (__contains__) operator returns a bool whose value is neither True nor False

As expected, 1 is not contained by the empty tuple >>> 1 in () False but the False value returned is not equal to False >>> 1 in () == False False Looking at it another way, the in operator returns a bool which is neither True nor False: >>>…
user2949478
  • 323
  • 2
  • 5
32
votes
6 answers

JavaScript - === vs == operators performance

A few weeks ago, I have read this thread Is < faster than <=? about comparison operators in C. It was said that there is no difference in the performance between < and <= as they are interpreted as same/similar machine commands. At the same time, in…
gotqn
  • 36,464
  • 39
  • 145
  • 218
1
2
3
43 44