[ create a new paste ] login | about

Link: http://codepad.org/8gI4wz2b    [ raw code | output | fork ]

C, pasted on Oct 3:
#include <stdio.h>
#include <math.h>

#define ROUND1(x) ((x)-(int)(x))>=0.5?ceil((x)):floor((x))
#define ROUND2(x) floor((x)+0.5)
#define ROUND3(x) (double)(int)((x) + 0.5 - (double)((x) < 0))
#define ROUND4(x) ((x)-floor(x))>=0.5?ceil((x)):floor((x))
#define ROUND5(x) ((x)>0?floor((x)+0.5):ceil((x)-0.5))

#define __T(x) #x
#define _T(x) __T(x)

#define DEF(y) \
    printf("#define " #y "(x) " _T(y(x)) "\n"); \
    printf(#y "( 1.49) = % f\n", y(1.49));  \
    printf(#y "( 1.5)  = % f\n", y(1.5));   \
    printf(#y "( 1.51) = % f\n", y(1.51));  \
    printf(#y "(-1.49) = % f\n", y(-1.49)); \
    printf(#y "(-1.5)  = % f\n", y(-1.5));  \
    printf(#y "(-1.51) = % f\n", y(-1.51)); \
    printf("\n");

int main(void)
{
    DEF(ROUND1);
    DEF(ROUND2);
    DEF(ROUND3);
    DEF(ROUND4);
    DEF(ROUND5);

    return 0;
}


Output:
#define ROUND1(x) ((x)-(int)(x))>=0.5?ceil((x)):floor((x))
ROUND1( 1.49) =  1.000000
ROUND1( 1.5)  =  2.000000
ROUND1( 1.51) =  2.000000
ROUND1(-1.49) = -2.000000
ROUND1(-1.5)  = -2.000000
ROUND1(-1.51) = -2.000000

#define ROUND2(x) floor((x)+0.5)
ROUND2( 1.49) =  1.000000
ROUND2( 1.5)  =  2.000000
ROUND2( 1.51) =  2.000000
ROUND2(-1.49) = -1.000000
ROUND2(-1.5)  = -1.000000
ROUND2(-1.51) = -2.000000

#define ROUND3(x) (double)(int)((x) + 0.5 - (double)((x) < 0))
ROUND3( 1.49) =  1.000000
ROUND3( 1.5)  =  2.000000
ROUND3( 1.51) =  2.000000
ROUND3(-1.49) = -1.000000
ROUND3(-1.5)  = -2.000000
ROUND3(-1.51) = -2.000000

#define ROUND4(x) ((x)-floor(x))>=0.5?ceil((x)):floor((x))
ROUND4( 1.49) =  1.000000
ROUND4( 1.5)  =  2.000000
ROUND4( 1.51) =  2.000000
ROUND4(-1.49) = -1.000000
ROUND4(-1.5)  = -1.000000
ROUND4(-1.51) = -2.000000

#define ROUND5(x) ((x)>0?floor((x)+0.5):ceil((x)-0.5))
ROUND5( 1.49) =  1.000000
ROUND5( 1.5)  =  2.000000
ROUND5( 1.51) =  2.000000
ROUND5(-1.49) = -1.000000
ROUND5(-1.5)  = -2.000000
ROUND5(-1.51) = -2.000000



Create a new paste based on this one


Comments: