Originally Posted by
Hodor
LOL (I'm not laughing at you, but you have to admit that's a pretty novel implementation of min/max)
Although your reasoning is somewhat dubious, I cannot fault you for trying different methods. It's cool that you've done this as an exercise. The technique you've "come up with" is useful so it's not as though you've wasted your time, and you've learned something so you've used your time doubly well. Even better, by asking a sensible question you've learnt even more!
I give you a Hodor Cookie as a reward.
Now, to see which is more efficient (not that it matters, but you may as well keep experimenting and thinking since you're on a roll) you can go two ways: a) you can look at the assembly generated and do some calculations; or b) you can take a more empirical approach and implement two test programs, perform min or max a million times or so for each program and time how long things take (e.g. time ./mymin and time ./mymin2). If you try option (b) be careful your compiler doesn't just optimise the loop (if you use a loop) away because you're not using intermediate results or something
hooray...!
well, it also works with double.
Code:
inline double fmax(double a, double b)
{
const double result[2] = { a, b };
return result[static_cast<unsigned int>(result[0] < result[1])];
}
inline double fmin(double a, double b)
{
const double result[2] = { a, b };
return result[static_cast<unsigned int>(result[0] > result[1])];
}