home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!ferkel.ucsb.edu!taco!gatech!destroyer!caen!saimiri.primate.wisc.edu!ames!pacbell.com!UB.com!pippen.ub.com!rfries
- From: rfries@sceng.ub.com (Robert Fries)
- Subject: Re: Rounding floats to integers
- Message-ID: <rfries.123@sceng.ub.com>
- Sender: news@pippen.ub.com (The Daily News)
- Nntp-Posting-Host: 128.203.1.151
- Organization: Ungermann Bass
- References: <92314.134711IA8@psuvm.psu.edu>
- Date: Tue, 10 Nov 1992 14:16:22 GMT
- Lines: 28
-
- In article <92314.134711IA8@psuvm.psu.edu> IA8@psuvm.psu.edu (Alan R. Heyd) writes:
-
- >What I do now is add 0.5 to the float before it is converted to
- >an integer, for example:
-
- To round to NEAREST integer, you'd have to test if fractional part is less
- than 0.5; if YES, drop fractional part, if NO, add 0.5, then drop fraction.
- You'd also get to choose what to do if fractional part is EXACTLY 0.5.
-
- Off the top of my head:
-
- int round(float v)
- {
- if ((v - (int)v) < 0.5) // use '<=' if 0.5 should round down
- return (int)v;
- return (int)(v+0.5);
- }
-
- Robert
-
- //////////////////////////////////////////////////////////////////////////
- Robert Fries
- Ungermann-Bass Inc.
-
- DISCLAIMER:
- Opinions contained herein are my own, and are not necessarily those
- of Ungermann-Bass.
- //////////////////////////////////////////////////////////////////////////
-