min
What is the difference between std::min/std::max and fmin/fmax?
fmin and fmax are specifically for use with floating point numbers (hence the “f”). If you use it for ints, you may suffer performance or precision losses due to conversion, function call overhead, etc. depending on your compiler/platform. std::min and std::max are template functions (defined in header <algorithm>) which work on any type with a … Read more
Remove Max and Min values from python list of integers
Here’s another way to do it if you don’t want to change the order of the items: mylist = [1, 4, 0, 3, 2] mylist.remove(max(mylist)) mylist.remove(min(mylist)) Assumes that the high/low don’t have any duplicates in the list, or if there are, that it’s OK to remove only one of them. This will need to do … Read more
MySQL: Select N rows, but with only unique values in one column
Probably not the most elegant of solutions, and the performance of IN may suffer on larger tables. The nested query gets the minimum Birthyear for each city. Only records who have this Birthyear are matched in the outer query. Ordering by age then limiting to 3 results gets you the 3 oldest people who are … Read more
Using min/max *within* an Integer Linear Program
Add in an auxiliary variable, say x4, with constraints: x4 >= c1*x1 x4 >= c2*x2 x4 >= c3*x3 Objective += c4*x4
std::max() and std::min() not constexpr
std::min and std::max are constexpr in C++14, which obviously means there isn’t a good reason (these days) not to have them constexpr. Problem solved 🙂
C extension:
Recent manuals say: The G++ minimum and maximum operators (‘<?’ and ‘>?’) and their compound forms (‘<?=’) and ‘>?=’) have been deprecated and are now removed from G++. Code using these operators should be modified to use std::min and std::max instead. A quick search of the past documents seems to indicate that they were removed … Read more
Fastest way to zero out low values in array?
This is a typical job for NumPy, which is very fast for these kinds of operations: array_np = numpy.asarray(array) low_values_flags = array_np < lowValY # Where values are low array_np[low_values_flags] = 0 # All low values set to 0 Now, if you only need the highCountX largest elements, you can even “forget” the small elements … Read more
Why does Math.min() return positive infinity, while Math.max() returns negative infinity?
Of course it would, because the start number should be Infinity for Math.min. All number that are lower than positive infinity should be the smallest from a list, if there are no smaller. And for Math.max it’s the same; all numbers that are larger than negative infinity should be the biggest if there are no … Read more
Why does Release/Debug have a different result for std::min?
In IEEE 754 comparing NAN to anything will always yield false, no matter what it is. slope > 0; // false slope < 0; // false slope == 0; // false And, more importantly for you slope < DBL_MAX; // false DBL_MAX < slope; // false So it seems that the compiler reorders the parameters/uses … Read more