473,386 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How can MIN macro without :? be written?

#define MIN(x, y) ((x)<(y) ? (x):(y))

Can a version without conditional operator of MIN macro be written?

Jan 17 '07 #1
11 4994
"lovecreatesbea...@gmail.com" <lo***************@gmail.comwrites:
#define MIN(x, y) ((x)<(y) ? (x):(y))

Can a version without conditional operator of MIN macro be written?
Here's an expression, without the obfuscation of a macro:
x * (x < y) + y * !(x < y)
--
"Welcome to the wonderful world of undefined behavior, where the demons
are nasal and the DeathStation users are nervous." --Daniel Fox
Jan 17 '07 #2
Ben Pfaff wrote:
"lovecreatesbea...@gmail.com" <lo***************@gmail.comwrites:
#define MIN(x, y) ((x)<(y) ? (x):(y))

Can a version without conditional operator of MIN macro be written?

Here's an expression, without the obfuscation of a macro:
x * (x < y) + y * !(x < y)
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.

Jan 17 '07 #3
lovecreatesbea...@gmail.com wrote:
#define MIN(x, y) ((x)<(y) ? (x):(y))

Can a version without conditional operator of MIN macro be written?
Yes, it can. I won't give you a solution, but point you in a direction.

Think about what the value of (x)<(y) is. How could you manipulate that
value, to give a value that is x if x is the minimum? How then could you
get y if y is the minimum? How can you combine them together? What
happens if they are the same value?

--
Simon.
Jan 17 '07 #4
"Harald van Dijk" <tr*****@gmail.comwrites:
Ben Pfaff wrote:
>"lovecreatesbea...@gmail.com" <lo***************@gmail.comwrites:
#define MIN(x, y) ((x)<(y) ? (x):(y))

Can a version without conditional operator of MIN macro be written?

Here's an expression, without the obfuscation of a macro:
x * (x < y) + y * !(x < y)

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.
I was assuming that x and y are integers. Generally that's what
programmers pass to a MIN macro.

This is another reason to avoid macros: people have more
surprising ways to use them than they do with functions.
--
"The lusers I know are so clueless, that if they were dipped in clue
musk and dropped in the middle of pack of horny clues, on clue prom
night during clue happy hour, they still couldn't get a clue."
--Michael Girdwood, in the monastery
Jan 17 '07 #5
"lovecreatesbea...@gmail.com" wrote:
>
#define MIN(x, y) ((x)<(y) ? (x):(y))

Can a version without conditional operator of MIN macro be written?
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.)

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Jan 17 '07 #6
Kenneth Brody <ke******@spamcop.netwrites:
"lovecreatesbea...@gmail.com" wrote:

#define MIN(x, y) ((x)<(y) ? (x):(y))

Can a version without conditional operator of MIN macro be written?

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.)
Yes, it's cheating, because you're ignoring things like integer
overflow, loss of floating point precision, pointers, and so on.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 17 '07 #7

"Keith Thompson" <ks***@mib.orgwrote in message
news:ln************@nuthaus.mib.org...
>(Yes, I'm ignoring things like integer overflow, loss of floating point
precision, pointers, and so on.)

Yes, it's cheating, because you're ignoring things like integer
overflow, loss of floating point precision, pointers, and so on.
So every line that does integer calculation ignoring overflow is wrong?
Jan 18 '07 #8
"Serve Laurijssen" <se*@n.tkwrites:
So every line that does integer calculation ignoring overflow is wrong?
Yes. Unsigned integers don't overflow (they wrap around), but
signed integer overflow yields undefined behavior. Smart
compilers can depend on the undefinedness of signed integer
overflow for optimization purposes, causing your code to actually
malfunction if an overflow occurs.
--
"Some programming practices beg for errors;
this one is like calling an 800 number
and having errors delivered to your door."
--Steve McConnell
Jan 18 '07 #9
"Serve Laurijssen" <se*@n.tkwrites:
"Keith Thompson" <ks***@mib.orgwrote in message
news:ln************@nuthaus.mib.org...
(Yes, I'm ignoring things like integer overflow, loss of floating point
precision, pointers, and so on.)
The above was written by Kenneth Brody <ke******@spamcop.net>.
Please don't snip attribution lines.
Yes, it's cheating, because you're ignoring things like integer
overflow, loss of floating point precision, pointers, and so on.

So every line that does integer calculation ignoring overflow is wrong?
No. The code in question was a MIN macro, presumably intended to be
general-purpose. Ignoring overflow in such cases is a bad idea,
especially since it's easy enough to compute MIN in a way that avoids
overflow altogether. (The most straightforward implementation doesn't
work with operands with side effects -- so don't invoke it with
arguments with side effects.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 19 '07 #10
Keith Thompson wrote:
>
Kenneth Brody <ke******@spamcop.netwrites:
"lovecreatesbea...@gmail.com" wrote:
>
#define MIN(x, y) ((x)<(y) ? (x):(y))
>
Can a version without conditional operator of MIN macro be written?
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.)

Yes, it's cheating, because you're ignoring things like integer
overflow, loss of floating point precision, pointers, and so on.
Not to mention the fact that MAX() is probably still written using
the ?: operator.

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.

That is:

given:

#define MAX(x,y) ( (x)+(y) - MIN(x,y) )
#define MIN(x,y) ( (x)+(y) - MAX(x,y) )

then:

MIN(x,y)

expands to:

( (x)+(y) - ( (x)+(y) - MIN(x,y) ) )

Is this defined by the standard as such?

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Jan 19 '07 #11
Kenneth Brody <ke******@spamcop.netwrites:
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.
From C99 6.10.3.4 (similar text is in C90):

If the name of the macro being replaced is found during this
scan of the replacement list (not including the rest of the
source file's preprocessing tokens), it is not replaced.
Furthermore, if any nested replacements encounter the name
of the macro being replaced, it is not replaced. These
nonreplaced macro name preprocessing tokens are no longer
available for further replacement even if they are later
(re)examined in contexts in which that macro name
preprocessing token would otherwise have been replaced.

--
"Large amounts of money tend to quench any scruples I might be having."
-- Stephan Wilms
Jan 19 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
36
by: Martin | last post by:
Can anyone help with a quick query... I've seen the ASSERT macro defined as: #define ASSERT(f) \ do { \ if (!(f) && assertFailedOnLine (THIS_FILE, __LINE__)) \ FatalExit (0); \ } while...
44
by: Simon Morgan | last post by:
Hi, Can somebody please help me grok the offsetof() macro? I've found an explanation on http://www.embedded.com/shared/printableArticle.jhtml?articleID=18312031 but I'm afraid it still...
12
by: Laurent Deniau | last post by:
I was playing a bit with the preprocessor of gcc (4.1.1). The following macros expand to: #define A(...) __VA_ARGS__ #define B(x,...) __VA_ARGS__ A() -nothing, *no warning* A(x) -x ...
45
by: madhawi | last post by:
Is it better to use a macro or a function?
10
by: gouqizi.lvcha | last post by:
Hi Friends, I saw a usage of macro like #define B3 "\xA\xB\xC" I don't understand why B3 is digital 10, can ayone point what the logic behind this usage. Rick
4
by: Gestorm | last post by:
Hi all, I found a macro "USE_VAR" in the code of bash-3.2 as follows: /*file: bash-3.2/shell.c*/ 344 USE_VAR(argc); 345 USE_VAR(argv); 346 USE_VAR(env); 347 USE_VAR(code); 348 ...
16
by: mdh | last post by:
I have asked a few questions about Macros...and think what I have been missing ...and which all who have replied clearly understand...is that this is a Pre-processor action. Hopefully the above is...
36
by: sh.vipin | last post by:
how to make large macro paste the code as it is Problem Explanation '-- For example in the program below /* a.c - starts here */ #define DECL_VARS() \ unsigned int a0;\ unsigned int a1;\...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.