TDD
const max12 = (x, y) => {
if (x === 1 && y === 2) {
return 2;
} else if (x === 7 && y === 4) {
return 7;
} else {
return x;
}
};
#define max(x,y) ( { __auto_type __x = (x); __auto_type __y = (y); __x > __y ? __x : __y; })
GNU C. Also works with Clang. Avoids evaluating the arguments multiple times. The optimizer will convert the branch into a conditional move, if it doesn’t I’d replace the ternary with the “bit hacker 2” version.
Mathematician 2 kinda blew my mind, kinda obvious, just can’t believe I was never taught or thought about it.
I’ve been staring at it for 10 minutes and I’m still not convinced it works.
Mathematician 3
Max(x, y) = floor(ln(e^x + e^y))
so 0.3 ~= 1-ln(2)=max(1-ln(2),1-ln(2)) = floor(ln(2*e^(1-ln(2)))) = floor(ln(2)+(1-ln(2))) = 1 ?
That would bee engeneer 2, not Mathematician3 xD.
Just out of curiostity, what was you Idea behind that?
Guess only work with integers, specially for the floor function that is going to give you an integer at the end everytime.
Not my idea, learned it somewhere while doing college in an statistics class. The idea is that the exponential function grow really fast, so small difference on variables become extreme difference on the exponential, then the log function reverse the exponential, but because it grew more for the biggest variable it reverts to the max variable making the other variables the decimal part (this is why you need the floor function). I think is cool because works for any number of variables, unlike mathematician 2 who only work for 2 variables (maybe it can be generalized for more variables but I don’t think can be done).
For a min fuction it can be use ceiling(-ln(e^-x + e^-y))
to be fair it does seem to work for any two numbers where one is >1. As lim x,y–> inf ln(ex+ey) <= lim x,y --> inf ln(2 e^(max(x,y))) = max(x,y) + ln(2).
I think is cool because works for any number of variables
using the same proof as before we can see that: lim,x_i -->inf ln(sum_i/in I} e^(x_i)) <= ln(.
So it would only work for at most [base of your log, so e<3 for ln] variables.
Reminded of how truly little I know about programming despite the time have spent doing it
Ugh. I’ll never be any good.