MIN and MAX in C

后端 未结 14 1680
攒了一身酷
攒了一身酷 2020-11-22 13:32

Where are MIN and MAX defined in C, if at all?

What is the best way to implement these, as generically and type safely as possible? (Compil

14条回答
  •  北荒
    北荒 (楼主)
    2020-11-22 14:28

    It's worth pointing out I think that if you define min and max with the ternary operation such as

    #define MIN(a,b) (((a)<(b))?(a):(b))
    #define MAX(a,b) (((a)>(b))?(a):(b))
    

    then to get the same result for the special case of fmin(-0.0,0.0) and fmax(-0.0,0.0) you need to swap the arguments

    fmax(a,b) = MAX(a,b)
    fmin(a,b) = MIN(b,a)
    

提交回复
热议问题