comparison-operators

JavaScript equality transitivity is weird

你。 提交于 2019-12-17 07:17:26
问题 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 // false false == null // false null == undefined // true The author also goes on to mention "to never use == and != . Instead, always use === and !== ". However, he doesn't explain why the above behavior is exhibited? So my question is, why are the above results as they are? Isn't

What is the Ruby <=> (spaceship) operator?

给你一囗甜甜゛ 提交于 2019-12-16 20:02:23
问题 What is the Ruby <=> (spaceship) operator? Is the operator implemented by any other languages? 回答1: Perl was likely the first language to use it. Groovy is another language that supports it. Basically instead of returning 1 ( true ) or 0 ( false ) depending on whether the arguments are equal or unequal, the spaceship operator will return 1 , 0 , or −1 depending on the value of the left argument relative to the right argument. a <=> b := if a < b then return -1 if a = b then return 0 if a > b

In Python, how to know whether objects can be compared?

本秂侑毒 提交于 2019-12-14 01:21:38
问题 With abstract base classes, Python provides a way to know the behavior of objects without actually trying it out. In the standard library, we have some ABCs defined for containers in collections.abc. For example, one can test that an argument is iterable: from collections.abc import Iterable def function(argument): if not isinstance(argument, Iterable): raise TypeError('argument must be iterable.') # do stuff with the argument I was hoping that there would be one such ABC for deciding whether

How do the OCaml operators < and > work with non-integer types?

痞子三分冷 提交于 2019-12-12 22:06:32
问题 I'm curious how the greater than (>) and less than (<) operators work with types that are not int, float, or double in OCaml. For instance, I was able to discover that string "a" > "b" but is there some reference that lists the conventions for all non-numerical data types. Furthermore, how do these operators work across types? e.g. Is "a" > true or is "a" < true? Finally, how would these work across a user-defined data type? Thanks! 回答1: The OCaml < , > , <= , >= operators only work with two

What does “compares less than 0” mean?

大兔子大兔子 提交于 2019-12-12 08:12:35
问题 Context While I was reading Consistent comparison, I have noticed a peculiar usage of the verb to compare : There’s a new three-way comparison operator, <=>. The expression a <=> b returns an object that compares <0 if a < b, compares >0 if a > b, and compares ==0 if a and b are equal/equivalent. Another example found on the internet (emphasis mine): It returns a value that compares less than zero on failure. Otherwise, the returned value can be used as the first argument on a later call to

Define how a comparison operator is applied to a type?

ⅰ亾dé卋堺 提交于 2019-12-12 04:38:51
问题 How can I define whether and how a comparison operator is applied to operands of my type? 回答1: You implement the IComparable interface with the CompareTo method. To use all of the operators, try this: public sealed class Foo : IEquatable<Foo>, IComparable<Foo> { public static int Compare(Foo first, Foo second) { if (Object.ReferenceEquals(first, null)) return (Object.ReferenceEquals(second, null) ? 0 : -1); return first.CompareTo(second); } public static bool operator==(Foo first, Foo second)

== vs. === in a remote JavaScript file. Which one is faster? [duplicate]

Deadly 提交于 2019-12-11 16:50:30
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: JavaScript === vs == : Does it matter which “equal” operator I use? When comparing two operands of the same type for equality in JavaScript, using == or === doesn't make any conceptual difference, so I'm wondering which operator is actually faster when, like in my case, a JavaScript file has to be downloaded from a remote Internet location. While the strict equality operator === may perform faster on many user

Shared pointer constness in comparison operator ==

瘦欲@ 提交于 2019-12-11 14:09:24
问题 I stumbled upon an unexpected behavior of a shared pointer I'm using. The shared pointer implements reference counting and detaches (e.g. makes a copy of), if neccessary, the contained instance on non-const usage. To achieve this, for each getter function the smart pointer has a const and a non-const version, for example: operator T *() and operator T const *() const . Problem: Comparing the pointer value to nullptr leads to a detach. Expected: I thought that the comparison operator would

Numeric comparison difficulty in R

♀尐吖头ヾ 提交于 2019-12-11 13:54:15
问题 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 this seems related: (a - b) == 0.5) is false, while all.equal((a - b), 0.5) is true. The only solution I can think of is to have two conditions: (a-b) > 0.5 | all.equal((a-b), 0.5) . This works, but is that really the only solution? Should I just

Associativity of comparison operators in Python

孤人 提交于 2019-12-11 13:00:18
问题 What is the associativity of comparison operators in Python? It is straightforward for three comparisons, but for more than that, I'm not sure how it does it. They don't seem to be right- or left-associative. For example: >>> 7410 >= 8690 <= -4538 < 9319 > -7092 False >>> (((7410 >= 8690) <= -4538) < 9319) > -7092 True So, not left-associative. >>> 81037572 > -2025 < -4722 < 6493 False >>> (81037572 > (-2025 < (-4722 < 6493))) True So it's not right-associative either. I have seen some places