L
lovecreatesbea...
#define MIN(x, y) ((x)<(y) ? (x)
y))
Can a version without conditional operator of MIN macro be written?
Can a version without conditional operator of MIN macro be written?
[email protected] said:#define MIN(x, y) ((x)<(y) ? (x)y))
Can a version without conditional operator of MIN macro be written?
Ben said:Here's an expression, without the obfuscation of a macro:
x * (x < y) + y * !(x < y)
[email protected] said:#define MIN(x, y) ((x)<(y) ? (x)y))
Can a version without conditional operator of MIN macro be written?
Harald van Dijk said:You're assuming that 1 * x == x, and that 0 * x == 0. This is not
necessarily true. For example, in floating point arithmetic, using your
expression with an infinity will result in NaN. For another example, if
x is a pointer, 1 * x and 0 * x are not valid expressions.
[email protected] said:#define MIN(x, y) ((x)<(y) ? (x)y))
Can a version without conditional operator of MIN macro be written?
Kenneth Brody said:Is this cheating?
#define MIN(x,y) ( (x)+(y) - MAX(x,y) )
(Yes, I'm ignoring things like integer overflow, loss of floating point
precision, pointers, and so on.)
Keith Thompson said:Yes, it's cheating, because you're ignoring things like integer
overflow, loss of floating point precision, pointers, and so on.
Serve Laurijssen said:So every line that does integer calculation ignoring overflow is wrong?
Serve Laurijssen said:So every line that does integer calculation ignoring overflow is wrong?
Keith said:Yes, it's cheating, because you're ignoring things like integer
overflow, loss of floating point precision, pointers, and so on.
Kenneth Brody said:What happens if you have a circular #define set? For example, if the
MAX() macro were also rewritten as above, referring to MIN()? My C
compiler expands MIN() by expanding MAX(), but leaving MAX's MIN()
intact. And MAX() expands MIN(), leaving its MAX() intact.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.