home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!caen!uwm.edu!csd4.csd.uwm.edu!eli
- From: eli@csd4.csd.uwm.edu (Elihu Lubkin)
- Newsgroups: comp.lang.c
- Subject: Rounding floats to integers
- Date: 10 Nov 1992 01:28:24 GMT
- Organization: University of Wisconsin - Milwaukee
- Lines: 35
- Message-ID: <1dn37oINNr42@uwm.edu>
- NNTP-Posting-Host: 129.89.7.4
-
- From uwm.edu!biosci!agate!usenet.ins.cwru.edu!gatech!rutgers!psuvax1!psuvm!ia8 Mon Nov 9 18:47:13 1992
- Path: uwm.edu!biosci!agate!usenet.ins.cwru.edu!gatech!rutgers!psuvax1!psuvm!ia8
- From: IA8@psuvm.psu.edu (Alan R. Heyd)
- Newsgroups: comp.lang.c
- Subject: Rounding floats to integers
- Message-ID: <92314.134711IA8@psuvm.psu.edu>
- Date: 9 Nov 92 18:47:11 GMT
- Organization: Penn State University
- Lines: 18
-
- >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
- >find mention of a routine which rounds to the nearest integer.
- >What I do now is add 0.5 to the float before it is converted to
- >an integer, for example:
- >
- > int i;
- > /* Using Turbo C on and IBM PC: */
- > i = 6.0 / 0.3; /* i = 19 */
- > i = 6.0 / 0.3 + 0.5; /* i = 20 as expected */
- >
-
- But if i = -6.0 / 0.3 + 0.5 then i = -19 so you also have to
- watch out for the sign.
- Thelma Lubkin
-
- >is there a better way?
- >
- >Thanks,
- >
- >Alan Heyd
- >ia8@psuvm.psu.edu
-
-
-