www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/05/29/20:31:17

From: Mbaccar AT aol DOT com
Message-ID: <ceaeddef.2481e064@aol.com>
Date: Sat, 29 May 1999 20:29:24 EDT
Subject: Re: how to round fp-numbers correctly ?
To: Mbaccar AT aol DOT com, djgpp AT delorie DOT com
MIME-Version: 1.0
X-Mailer: AOL 4.0 for Windows sub 11
Reply-To: djgpp AT delorie DOT com

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019