[Numpy-discussion] Broken tMIN/tMAX macros

Sasha ndarray at mac.com
Fri Jul 7 17:41:27 CDT 2006

Travis' recent change
<http://projects.scipy.org/scipy/numpy/changeset/2771> highlighted the
definitions of tMIN/tMAX macros.

Typed min/max were a subject for some heated discussion between Linux
kernel developers  many years ago
<http://lwn.net/2001/0823/kernel.php3> that resulted in the following
definitions  in the curent kernel:

 * min()/max() macros that also do
 * strict type-checking.. See the
 * "unnecessary" pointer comparison.
#define min(x,y) ({ \
        typeof(x) _x = (x);     \
        typeof(y) _y = (y);     \
        (void) (&_x == &_y);            \
        _x < _y ? _x : _y; })

#define max(x,y) ({ \
        typeof(x) _x = (x);     \
        typeof(y) _y = (y);     \
        (void) (&_x == &_y);            \
        _x > _y ? _x : _y; })

 * ..and if you can't take the strict
 * types, you can specify one yourself.
 * Or not use min/max at all, of course.
#define min_t(type,x,y) \
        ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
#define max_t(type,x,y) \
        ({ type __x = (x); type __y = (y); __x > __y ? __x: __y; })

The idea is to force people to use _t versions unless the types of x
and y are exactly the same.

The numpy's tMIN and tMAX are clearly addressing the same problem, but
current definitions

#define tMAX(a,b,typ) {typ _x_=(a); typ _y_=(b); _x_>_y_ ? _x_ : _y_}
#define tMIN(a,b,typ) {typ _x_=(a); typ _y_=(b); _x_<_y_ ? _x_ : _y_}

are unlikely to work with any compiler.  Linux kernel uses gcc trick
of wrapping a block in parenthesis to get an expression but I don't
think this is acceptable in numpy code.

Not surprizingly, these macros are not used anywhere.  I propose to remove them.

More information about the Numpy-discussion mailing list