Median of 5 sorted arrays

(This is a generalization of your idea for two arrays.) If you start by looking at the five medians of the five arrays, obviously the overall median must be between the smallest and the largest of the five medians. Proof goes something like this: If a is the min of the medians, and b is … Read more

Most efficient/elegant way to clip a number?

What about boring, old, readable, and shortest yet: float clip(float n, float lower, float upper) { return std::max(lower, std::min(n, upper)); } ? This expression could also be ‘genericized’ like so: template <typename T> T clip(const T& n, const T& lower, const T& upper) { return std::max(lower, std::min(n, upper)); } Update Billy ONeal added: Note that … Read more

Incrementor logic

Quoting Java Language Specification, 15.7 Evaluation Order: The Java programming language guarantees that the operands of operators appear to be evaluated in a specific evaluation order, namely, from left to right. The left-hand operand of a binary operator appears to be fully evaluated before any part of the right-hand operand is evaluated. If the operator … Read more