common define in c/c++

#define MIN(v1, v2)         ( ((v1) > (v2)) ? (v2) : (v1) )
#define MAX(v1, v2)         ( ((v1) < (v2)) ? (v2) : (v1) )
#define ABS(v)              ( ((v) < 0)     ? -(v) : (v)  )
#define SQR(v)              ( (v) * (v) )
#define ROUND(v)            ( (int) ((v)+0.5) )
#define TRUNC(v)            ( (int) (v) )
#define EQUAL_ZERO(v,tol)      ( ABS(v) < tol )
#define DIST_2D(x1, y1, x2, y2) (sqrt(SQR((x1)-(x2))+SQR((y1)-(y2))))
#define PI          3.141592654


    /* Random number between 0 and 1 */
#define RANDOM   (fabs(((double)random())/RAND_MAX))

 

    /* Number of elements in an array */
#define NELEM(v) (sizeof(v)/sizeof(v[0]))

/*************** Commonly used string macros *****************/
/*#define STRING_TO_DOUBLE(s, d) {                               /
    char* test;                                                /
    (d) = strtod((s),&test);                                   /
    if(test == (s)){                                           /
        printf("Error converting string to double: %s",(s));   /
        exit(1);                                               /
    }                                                         /
}*/

/*#define STRING_TO_INT(s, i) {                                 /
    char* test;                                               /
    (i) = strtod((s),&test);                                  /
    if(test == (s)){                                          /
        printf("Error converting string to int: %s",(s));     /
        exit(1);                                              /
    }                                                         /
}*/

posted on 2011-03-19 21:14  zzsbest  阅读(200)  评论(0编辑  收藏  举报