Use of "min" and "max" functions

  • Hi,

    I'm trying to use the "min" and "max" functions.  These just return the
    smallest or
    largest value of the two passed aguments.

    I'm getting a link error when I try and use "min(val1, val2)" and get a
    ZeroLink
    error on _min and _max.

    What is the best way to deal with this?

    John
  • On 18 May 07, at 15:43, John Draper wrote:
    > I'm trying to use the "min" and "max" functions.  These just return
    > the smallest or
    > largest value of the two passed aguments.
    >
    > I'm getting a link error when I try and use "min(val1, val2)" and
    > get a ZeroLink
    > error on _min and _max.

    Those functions don't exist in that form. There are fmin/fminl/fminf,
    which operate on doubles, long doubles, and floating-point values,
    respectively. If you need one that operates on integers, though,
    you'll have to define it yourself.

    The best possible definition for integers is probably (in a header
    file):

    inline extern min(int a, int b) __attribute__((always_inline));
    inline extern min(a, b) { return a < b ? a : b; }
    inline extern max(int a, int b) __attribute__((always_inline));
    inline extern max(a, b) { return a > b ? a : b; }

    (The always_inline attribute causes the functions to always be
    inlined regardless of the optimization mode, obviating the need for a
    non-extern definition somewhere in the program. Found this in the GCC
    manual, section 5.36 "An Inline Function is As Fast As a Macro".)
  • > Ok,  I see - I seemed to remember there also was some type of standard
    > library they might be in,  but this is obviously the better choice.

    There's the STL templates min & max. Just remember that "return a > b ? a :
    b;" can evaluate the greater argument twice, so if you embed a call with
    side effects...

    --
    Scott Ribe
    <scott_ribe...>
    http://www.killerbytes.com/
    (303) 722-0567 voice
  • On May 18, 2007, at 4:43 PM, John Draper wrote:

    > What is the best way to deal with this?

    You may want to also try MIN() and MAX() (all upper case), which are
    implemented in the Foundation headers as #define macros.

    Nick Zitzmann
    <http://www.chronosnet.com/>
  • Quoting Andrew Farmer <andfarm...>:

    > On 18 May 07, at 15:43, John Draper wrote:
    >> I'm trying to use the "min" and "max" functions.  These just return
    >> the smallest or
    >> largest value of the two passed aguments.
    >>
    >> I'm getting a link error when I try and use "min(val1, val2)" and
    >> get a ZeroLink
    >> error on _min and _max.
    >
    > Those functions don't exist in that form. There are fmin/fminl/fminf,
    > which operate on doubles, long doubles, and floating-point values,
    > respectively. If you need one that operates on integers, though, you'll
    > have to define it yourself.
    >
    > The best possible definition for integers is probably (in a header file):
    >
    > inline extern min(int a, int b) __attribute__((always_inline));
    > inline extern min(a, b) { return a < b ? a : b; }
    > inline extern max(int a, int b) __attribute__((always_inline));
    > inline extern max(a, b) { return a > b ? a : b; }
    >
    > (The always_inline attribute causes the functions to always be inlined
    > regardless of the optimization mode, obviating the need for a
    > non-extern definition somewhere in the program. Found this in the GCC
    > manual, section 5.36 "An Inline Function is As Fast As a Macro".)

    Ok,  I see - I seemed to remember there also was some type of standard
    library they might be in,  but this is obviously the better choice.
  • On 5/18/07, John Draper <lists...> wrote:
    > Hi,
    >
    > I'm trying to use the "min" and "max" functions.  These just return the
    > smallest or
    > largest value of the two passed aguments.
    >
    > I'm getting a link error when I try and use "min(val1, val2)" and get a
    > ZeroLink
    > error on _min and _max.
    >
    > What is the best way to deal with this?

    Since you are asking on the Cocoa list... use MAX(), MIN(), etc. since
    those are defined by Foundation.

    -Shawn
previous month may 2007 next month
MTWTFSS
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      
Go to today