equality

Best way to avoid “isn't numeric in numeric eq (==)”-warning

耗尽温柔 提交于 2019-12-04 05:09:15
#!/usr/bin/env perl use warnings; use 5.12.2; my $c = 'f'; # could be a number too if ( $c eq 'd' || $c == 9 ) { say "Hello, world!"; } What is the best way, to avoid the 'Argument "f" isn't numeric in numeric eq (==) at ./perl.pl line 7.'-warning? I suppose in this case I could use "eq" two times, but that doesn't look good. Not sure why you want to avoid the warning. The warning is telling you that there's a potential problem in your program. If you're going to compare a number with a string that contains unknown data, then you're either going to have to use 'eq' for the comparison or clean

javascript truthy numbers

你。 提交于 2019-12-04 04:57:06
问题 Based on these rules: Falsy: false 0 (zero) '' or "" (empty string) null undefinded NaN (e.g. the result of 1/0) Truthy: Everything else I fail to find the correct explanation as to why in following tests, only number 1 evaluates to "true" 0 == true ("false") 1 == true ("true") 2 == true ("false") othernumber == true ("false") 回答1: The "truthy" and "falsy" rules only apply when the value itself is being used as the test, e.g.: var str = ""; if (str) { // It's truthy } else { // It's falsy } =

Python 2: different meaning of the 'in' keyword for sets and lists

ぃ、小莉子 提交于 2019-12-04 04:39:35
Consider this snippet: class SomeClass(object): def __init__(self, someattribute="somevalue"): self.someattribute = someattribute def __eq__(self, other): return self.someattribute == other.someattribute def __ne__(self, other): return not self.__eq__(other) list_of_objects = [SomeClass()] print(SomeClass() in list_of_objects) set_of_objects = set([SomeClass()]) print(SomeClass() in set_of_objects) which evaluates to: True False Can anyone explain why the 'in' keyword has a different meaning for sets and lists? I would have expected both to return True, especially when the type being tested

String Equality operator == in c#

青春壹個敷衍的年華 提交于 2019-12-04 04:34:13
问题 I have tried to peek into the code implemented for comparison operator in string class in C#. What found was this: //THIS IS NOT WHAT I MEANT public static bool Equals(object objA, object objB) { return ((objA == objB) || (((objA != null) && (objB != null)) && objA.Equals(objB))); } //THIS IS WHAT I SEE REALLY and the above is what I would expect to see public static bool Equals(string a, string b) { return ((a == b) || (((a != null) && (b != null)) && EqualsHelper(a, b))); } public static

Why aren't IStructuralEquatable and IStructuralComparable generic?

风流意气都作罢 提交于 2019-12-04 04:17:38
System.Collections.IStructuralEquatable and System.Collections.IStructuralComparable were added in .NET 4, but why aren't they generic, like IEquatable<T> and IComparable<T> ? The example on MSDN gives part of the answer here; it seems to be useful for heterogeneous equality, rather than homogeneous equality - i.e. for testing whether two objects (/values) of potentially different types should be considered equal. In such scenarios, it is extremely likely that the calling code is dealing with object (to represent heterogeneous data). And generic methods don't play nicely then. 来源: https:/

Will the var members in case class affect case class's equality?

半世苍凉 提交于 2019-12-04 04:11:22
I have made heavy use of case classes in my code, replying on the underlying equality definitions of case class to behave correctly. Then now I found that I need to add another field member to a case class. So if I add a var field member in case class, will it mess up the equality attributes for the case class? If 1 is yes, then what if I just change the var field value once, after that, no any reassignment will happen, before the case class goes into any collections or do equality comparison, will that still mess up the equality behaviors? Case class equality is based solely on its primary

checking for equality between an int and float in C

扶醉桌前 提交于 2019-12-04 04:07:37
问题 I came across this piece of code : int x=3; float y=3.0; if(x==y) printf("x and y are equal"); else printf("x and y are not equal"); Why does this code print "x and y are equal"?? Here if y=3.1(say), then the code prints "x and y are not equal". Someone please explain how is this happening. 回答1: Comparisons between arithmetic types are subject to the so-called usual arithmetic conversions (§5/9, §5.9/2, §5.10/1). Emphasis mine. Many binary operators that expect operands of arithmetic or

Logical equality in C

て烟熏妆下的殇ゞ 提交于 2019-12-04 03:15:31
问题 [It seems odd this doesn't exist, so apologies in advance if it's a duplicate] I want to test for logical equality in C. In other words, I want to know whether two values would be equal if both were converted in the normal way associated with logical expressions. In C99, I think that (bool)a == (bool)b gives what I want. Is that correct? What is the normal way of writing this in traditional C? 回答1: You typically see this: if ((a == 0) == (b == 0)) Or if (!!a == !!b) Since !!a evaluates to 1

Why a generic type definition implemented interfaces lose type information?

情到浓时终转凉″ 提交于 2019-12-04 03:01:51
问题 For example, if you run the following code... Type IListType = new List<string>().GetType() .GetInterface("IList`1") .GetGenericTypeDefinition(); ...and you watch IListType variable, you'll find that the whole Type instance has all properties available like FullName and others. But what happens when you run the code bellow? Type IListType2 = typeof(List<>).GetInterface("IList`1") Now IListType got from a generic type definition isn't the same as the first code sample: most Type properties

Finding duplicate entries in Collection

懵懂的女人 提交于 2019-12-04 02:03:16
Is there a tool or library to find duplicate entries in a Collection according to specific criteria that can be implemented? To make myself clear: I want to compare the entries to each other according to specific criteria. So I think a Predicate returning just true or false isn't enough. I can't use equals . It depends on the semantic of the criterion: If your criterion is always the same for a given class, and is inherent to the underlying concept , you should just implement equals and hashCode and use a set. If your criterion depend on the context , org.apache.commons.collections