home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- 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
- From: jon@stekt.oulu.fi (Jouni Miettunen)
- Subject: Re: Rounding floats to integers
- In-Reply-To: alien@acheron.amigans.gen.nz's message of 13 Nov 92 17:06:30 GMT+12
- Message-ID: <JON.92Nov15235738@stekt5.oulu.fi>
- Lines: 20
- Sender: news@ousrvr.oulu.fi
- Organization: University of Oulu, Dept. of EE, Finland
- References: <1dn37oINNr42@uwm.edu> <alien.010i@acheron.amigans.gen.nz>
- Date: Sun, 15 Nov 1992 21:57:38 GMT
-
- In article <alien.010i@acheron.amigans.gen.nz> alien@acheron.amigans.gen.nz (Ross Smith) writes:
-
- > >>Is there a C routine that will round off floating point numbers
- > >>to the nearest integer. K&R says that floats are converted to
- > >>integers by removing the fractional part, however, I could not
-
- I once needed a rint(3m) function when trying to port unix C to ANSI C
- and this is what I used:
-
- #include <math.h>
- double rint(register double f)
- {
- return (f >= 0) ? floor(f + 0.5) : ceil(f - 0.5);
- }
-
- It seemed to return correct values,
-
- --jouni
-
-
- --
-
- * Jouni Miettunen jon@stekt.oulu.fi * Oulu * Finland * Europe *
- *** Good fortune will find you, providing you gave directions ***
-