Image comparison – fast algorithm

Below are three approaches to solving this problem (and there are many others). The first is a standard approach in computer vision, keypoint matching. This may require some background knowledge to implement, and can be slow. The second method uses only elementary image processing, and is potentially faster than the first approach, and is straightforward … Read more

How are strings compared?

From the docs: The comparison uses lexicographical ordering: first the first two items are compared, and if they differ this determines the outcome of the comparison; if they are equal, the next two items are compared, and so on, until either sequence is exhausted. Also: Lexicographical ordering for strings uses the Unicode code point number … Read more

Why is 128==128 false but 127==127 is true when comparing Integer wrappers in Java?

When you compile a number literal in Java and assign it to a Integer (capital I) the compiler emits: Integer b2 =Integer.valueOf(127) This line of code is also generated when you use autoboxing. valueOf is implemented such that certain numbers are “pooled”, and it returns the same instance for values smaller than 128. From the … Read more

How to compare dates in Java? [duplicate]

Date has before and after methods and can be compared to each other as follows: if(todayDate.after(historyDate) && todayDate.before(futureDate)) { // In between } For an inclusive comparison: if(!historyDate.after(todayDate) && !futureDate.before(todayDate)) { /* historyDate <= todayDate <= futureDate */ } You could also give Joda-Time a go, but note that: Joda-Time is the de facto standard … Read more

What is the rationale for all comparisons returning false for IEEE754 NaN values?

I was a member of the IEEE-754 committee, I’ll try to help clarify things a bit. First off, floating-point numbers are not real numbers, and floating-point arithmetic does not satisfy the axioms of real arithmetic. Trichotomy is not the only property of real arithmetic that does not hold for floats, nor even the most important. … Read more

How do I do a case-insensitive string comparison?

Assuming ASCII strings: string1 = ‘Hello’ string2 = ‘hello’ if string1.lower() == string2.lower(): print(“The strings are the same (case insensitive)”) else: print(“The strings are NOT the same (case insensitive)”) As of Python 3.3, casefold() is a better alternative: string1 = ‘Hello’ string2 = ‘hello’ if string1.casefold() == string2.casefold(): print(“The strings are the same (case insensitive)”) … Read more

How does Python 2 compare string and int? Why do lists compare as greater than numbers, and tuples greater than lists?

From the python 2 manual: CPython implementation detail: Objects of different types except numbers are ordered by their type names; objects of the same types that don’t support proper comparison are ordered by their address. When you order two strings or two numeric types the ordering is done in the expected way (lexicographic ordering for … Read more

Sorting in JavaScript: Shouldn’t returning a boolean be enough for a comparison function?

TL;DR I have always successfully sorted my arrays like this No, you have not. And didn’t notice it. A quick counterexample: > [1,1,0,2].sort(function(a, b){ return a>b }) Array [0, 1, 2, 1] // in Opera 12. Results may vary between sorting algorithm implementations why? Because your comparison function does return false (or 0, equivalently) even … Read more

Why does comparing strings using either ‘==’ or ‘is’ sometimes produce a different result?

is is identity testing, == is equality testing. what happens in your code would be emulated in the interpreter like this: >>> a=”pub” >>> b = ”.join([‘p’, ‘u’, ‘b’]) >>> a == b True >>> a is b False so, no wonder they’re not the same, right? In other words: a is b is the … Read more

Object comparison in JavaScript [duplicate]

Unfortunately there is no perfect way, unless you use _proto_ recursively and access all non-enumerable properties, but this works in Firefox only. So the best I can do is to guess usage scenarios. 1) Fast and limited. Works when you have simple JSON-style objects without methods and DOM nodes inside: JSON.stringify(obj1) === JSON.stringify(obj2) The ORDER … Read more