home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / c / 16571 < prev    next >
Encoding:
Text File  |  1992-11-15  |  1.3 KB  |  37 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!ferkel.ucsb.edu!taco!rock!stanford.edu!agate!spool.mu.edu!caen!saimiri.primate.wisc.edu!ames!haven.umd.edu!darwin.sura.net!paladin.american.edu!news.univie.ac.at!hp4at!mcsun!news.funet.fi!ousrvr.oulu.fi!ousrvr!jon
  3. From: jon@stekt.oulu.fi (Jouni Miettunen)
  4. Subject: Re: Rounding floats to integers
  5. In-Reply-To: alien@acheron.amigans.gen.nz's message of 13 Nov 92 17:06:30 GMT+12
  6. Message-ID: <JON.92Nov15235738@stekt5.oulu.fi>
  7. Lines: 20
  8. Sender: news@ousrvr.oulu.fi
  9. Organization: University of Oulu, Dept. of EE, Finland
  10. References: <1dn37oINNr42@uwm.edu> <alien.010i@acheron.amigans.gen.nz>
  11. Date: Sun, 15 Nov 1992 21:57:38 GMT
  12.  
  13. In article <alien.010i@acheron.amigans.gen.nz> alien@acheron.amigans.gen.nz (Ross Smith) writes:
  14.  
  15. > >>Is there a C routine that will round off floating point numbers
  16. > >>to the nearest integer.  K&R says that floats are converted to
  17. > >>integers by removing the fractional part, however, I could not
  18.  
  19. I once needed a rint(3m) function when trying to port unix C to ANSI C
  20. and this is what I used:
  21.  
  22. #include <math.h>
  23. double rint(register double f)
  24. {
  25.     return (f >= 0) ? floor(f + 0.5) : ceil(f - 0.5);
  26. }
  27.  
  28. It seemed to return correct values,
  29.  
  30. --jouni
  31.  
  32.  
  33. --
  34.  
  35. * Jouni Miettunen  jon@stekt.oulu.fi  * Oulu * Finland * Europe *
  36. *** Good fortune will find you, providing you gave directions ***
  37.