home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!ornl!sunova!linac!uwm.edu!wupost!darwin.sura.net!Sirius.dfn.de!math.fu-berlin.de!hamel!rene
- From: rene@hamel.uucp (Rene Mueller)
- Subject: Re: Rounding floats to integers
- Message-ID: <MU286SO@math.fu-berlin.de>
- Sender: rene@hamel (Rene Mueller)
- Organization: Free University of Berlin, Germany
- References: <92314.134711IA8@psuvm.psu.edu>
- Date: Thu, 19 Nov 1992 17:45:23 GMT
- Lines: 26
-
- In article <92314.134711IA8@psuvm.psu.edu>, IA8@psuvm.psu.edu (Alan R. Heyd) 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
- |> 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 */
- |>
- |> is there a better way?
- |>
- |> Thanks,
- |>
- |> Alan Heyd
- |> ia8@psuvm.psu.edu
- Special roundfunctions, if you have any within your libraries, do it the same way.
- Cause it is the fastest solution to ROUND...
- But converting to integer just means: forget all behind the decimalpoint....
- So i = 6.0 / 0.3; is 'real' converting to integer and will produce on every
- machine with any compiler i = 19!
-
- Rene
-