Mail Archives: djgpp/1999/05/29/20:31:17
In a message dated 5/29/99 7:11:46 PM EST, Mbaccar writes:
> > How about:
> > #define ROUND(x) ((x) + SGN(x)*0.5)
>
> Looks good to me. Probably what you could do also is
> sgn(x) * ((int) fabs(x) + 0.5)
Actually the above will not work if you take 0.6 and -0.4 as input (sorry).
The first macro I posted will not work either. You have to apply
a constant shift to map all values to the positive or negative scale:
(int) (x - min + 0.5) - min
(int) (min -x -0.5) + min
(int) (max - x + 0.5) -max
(int) (x -max -0.5) + max
are the 4 options that work. They provide different results though,
so you should use the one with the behavior you need. I.E, does
0.5 map to zero or 1?
- Raw text -