equality

How to distinguish MethodBase in generics

筅森魡賤 提交于 2019-12-20 03:19:11
问题 I have a cache based on Dictionary<MethodBase, string> The key is rendered from MethodBase.GetCurrentMethod. Everything worked fine until methods were explicitly declared. But one day it is appeared that: Method1<T>(string value) Makes same entry in Dictionary when T gets absolutely different types. So my question is about better way to cache value for generic methods. (Of course I can provide wrapper that provides GetCache and equality encountered generic types, but this way doesn't look

Floating-point equality test and extra precision: can this code fail?

泪湿孤枕 提交于 2019-12-20 01:11:25
问题 The discussion started under my answer to another question. The following code determines machine epsilon : float compute_eps() { float eps = 1.0f; while (1.0f + eps != 1.0f) eps /= 2.0f; return eps; } In the comments it was proposed that the 1.0f + eps != 1.0f test might fail because C++ standard permits the use of extra precision. Although I'm aware that floating-point operations are actually performed in higher precision (than specified by the actual types used), I happen to disagree with

When comparing for equality is it okay to use `==`?

我只是一个虾纸丫 提交于 2019-12-19 19:44:23
问题 When comparing for equality is it okay to use == ? For example: int a = 3; int b = 4; If checking for equality should you use: if (a == b) { . . . } Would the situation change if floating point numbers were used? 回答1: ' == ' is perfectly good for integer values. You should not compare floats for equality; use an tolerance approach: if (fabs(a - b) < tolerance) { // a and b are equal to within tolerance } 回答2: Re floating points: yes. Don't use == for floats (or know EXACTLY what you're doing

Comparing structs for equality without boxing

北战南征 提交于 2019-12-19 17:34:34
问题 I came across an extension method that applies to structs (SomeStruct) and returns whether or not the value is equal to the default(SomeStruct) (when the parameterless constructor is called). public static bool IsDefault<T> (this T value) where T : struct { return (!EqualityComparer<T>.Default.Equals(value, default(T))); } This got me wondering whether the struct was being boxed. This is purely out of curiosity as there are pros/cons to boxing/passing by value depending on the context.

Is the == operator transitive in PHP?

◇◆丶佛笑我妖孽 提交于 2019-12-19 16:51:13
问题 In JavaScript, the == operator isn't necessarily transitive: js> '0' == 0 true js> 0 == '' true js> '0' == '' false Is the same true in PHP? Can you give an example? 回答1: No , the == operator is not transitive. The exact same scenario gives the same result in PHP. echo var_dump('0'==0); echo var_dump(0==''); echo var_dump('0'==''); yields: boolean true boolean true boolean false 回答2: The same is true in PHP: //php '0'==0 //true 0=='' //true ''=='0' //false Did you not test it yourself? These

What are Mocha equal tests?

 ̄綄美尐妖づ 提交于 2019-12-19 09:15:32
问题 I am testing an Express Node app with Mocha. I would like to have the following test (comparing two empty arrays): assert.equal [], [] to pass. However, Mocha gives me the following error: AssertionError: [] == [] Which method should I use in order for comparison of two empty arrays to pass? 回答1: The problem is that an array is a reference type in JavaScript, and hence only the reference is compared. And, of course, if you create two different empty arrays independent of each other, they are

set equality in linq

只愿长相守 提交于 2019-12-19 06:01:21
问题 I have two lists A and B (List). How to determine if they are equal in the cheapest way? I can write something like '(A minus B) union (B minus A) = empty set' or join them together and count amount of elements, but it is rather expensive. Is there workaround? 回答1: Well, that depends on how you interpret your lists. If you consider them as tuples (so the order of elements in lists matters), then you can go with this code: public bool AreEqual<T>(IList<T> A, IList<T> B) { if (A.Count != B

Is it ok to compare floating points to 0.0 without epsilon?

北战南征 提交于 2019-12-19 05:11:05
问题 I am aware, that to compare two floating point values one needs to use some epsilon precision, as they are not exact. However, I wonder if there are edge cases, where I don't need that epsilon. In particular, I would like to know if it is always safe to do something like this: double foo(double x){ if (x < 0.0) return 0.0; else return somethingelse(x); // somethingelse(x) != 0.0 } int main(){ int x = -3.0; if (foo(x) == 0.0) { std::cout << "^- is this comparison ok?" << std::endl; } } I know

Equality test of images using ImageMagick

我的梦境 提交于 2019-12-19 04:14:28
问题 Is there any equality predicate function in ImageMagick library? I want to compare two images and find whether they are the exactly same (all colors of the pixels are the same) or have any differences. I’ve looked around, but it seems not to have a such function. Should I write the function using pixel iterators by myself? 回答1: ImageMagick provides the compare function to properly compare images. Checking the md5 checksum of two images is not the correct approach, since some image formats (e

How is “sameness” defined for Java objects?

 ̄綄美尐妖づ 提交于 2019-12-19 04:02:06
问题 I want to add objects of a custom type to a Set. I have several which are the same, i.e. they have the same values for their public variables. I don't want more than one instance of a "same" object to be added to the set, but each time a new object is created it is always added. This is because the equals method for class Object implements the most discriminating possible equivalence relation on objects: "For any non-null reference values x and y, this method returns true if and only if x and