Not entirely related to the post, but what's the sane use case for doing: return (expr, expr) like in the first example? I cannot think of a good reason to do this.
It may not be a good reason to do this, but you can condense if (failure) { errno = EINVAL; return -1; } into if (failure) return (errno = EINVAL, -1);
The MIN Challenge
21–27 of 27 posts
Re: The MIN Challenge
#22Meanwhile, in C++14: auto min = [] (auto a, auto b) { return a
min x y = Data.Bool.bool x y (x > y)
Although considering what the article was talking about, doing stuff like this only makes you sound like an ass.Re: The MIN Challenge
#23Meanwhile, in C++14: auto min = [] (auto a, auto b) { return a
Meanwhile, in Haskell: min x y = Data.Bool.bool x y (x > y) Although considering what the article was talking about, doing stuff like this only makes you sound like an ass.
* http://msdn.microsoft.com/en-us/library/windows/desktop/dd75...
* http://msdn.microsoft.com/en-us/library/windows/desktop/dd75...
* https://stackoverflow.com/questions/22240973/major-and-minor...
Re: The MIN Challenge
#24Macros in C can sometimes be nasty, but "min" is a pathological case for a macro because it isn't what macros were meant for. In reality "min" should just be a function, and you should write a separate function for each type you want to define it over. This is how this problem is solved in C, and almost all other languages. It just looks deceptive because C's native operators like " " happen to be generic across some…
This is not how it's solved in any languages with any degree of polymorphism. Of course, C is entirely devoid of polymorphism – except for its math operators like `That's all fine, right? Well, it's fine if you're ok with writing every function four times. And that's only dealing with polymorphism in one argument. Hint: it's not at all uncommon for math functions to take multiple arguments. This is precisely why the first thing that a principled approach to mathematical programming does is allow polymorphism – ideally on multiple arguments. This is why people do math work in dynamic languages with lots of polymorphism, like Mathematica, Matlab, R and Python. Otherwise you can't get any work done without writing everything dozens of times – or struggling with C macro programming that makes this min puzzle look utterly tame. Sussman et al.'s SICM (SICP but for classical mechanics), not content with a dynamic language, immediately builds a multiple dispatch system on top of Scheme. R supports an awkward multiple dispatch system in one of its four object systems. This is why Guy Steele included multiple dispatch in Fortress. This is why Julia has multiple dispatch as its core paradigm. You need it for math. Badly.
Re: The MIN Challenge
#25 int _a = 50;
int result = min(num, _a);
you end up expanding to "int _a = _a;" which creates a new int _a, and assigns it to itself.Re: The MIN Challenge
#26An essential property of MIN is that it is symmetric with respect to its arguments: MIN(a, b) and MIN(b, a) are the same thing. But the expression (a We could assert that if only one value is an NaN, we return the other value. We might write it like this:
if (a != a) return b;
return a
This isn't symmetric if both arguments are different NaNs, but in practice getting different NaNs takes some work. (Note that C99 requires that fmin behave this way with respect to NaN.)But we're still not quite symmetric, thanks to negative zero. Negative zero compares equal to positive zero, but it doesn't seem right that the sign bit of the result should depend on the argument order.
glibc doesn't bother to handle negative zero, but Java's Math.min does:
public static double min(double a, double b) {
if (a != a) return a;
if ((a == 0.0d) && (b == 0.0d)
&& (Double.doubleToLongBits(b) == negativeZeroDoubleBits)) {
return b;
}
return (a
Delightful. Java has the best min.Re: The MIN Challenge
#27Macros in C can sometimes be nasty, but "min" is a pathological case for a macro because it isn't what macros were meant for. In reality "min" should just be a function, and you should write a separate function for each type you want to define it over. This is how this problem is solved in C, and almost all other languages. It just looks deceptive because C's native operators like " " happen to be generic across some…
"min" is a pathological case for a macro because it isn't what macros were meant for. Tell that to Kernighan and Ritchie. The second macro in K&R (I googled a PDF of the second edition) is #define max(A, B) ((A) > (B) ? (A) : (B)) Yes, that's lowercase 'max', and they even mention "This macro will work for any data type; there is no need for different kinds of max for different data types, as there would be with func…
max(a++, b--)
Which expands to ((a++)>(b--)?(a++):(b--))
creating unexpected results. The solution is, don't use return values of `++` and `--` in calls, unless you know it's a function, and always will be. Upper-case macros may help, but then you need to make sure all macros are upper case.