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

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!ornl!sunova!linac!uwm.edu!wupost!darwin.sura.net!Sirius.dfn.de!math.fu-berlin.de!hamel!rene
  3. From: rene@hamel.uucp (Rene Mueller)
  4. Subject: Re: Rounding floats to integers
  5. Message-ID: <MU286SO@math.fu-berlin.de>
  6. Sender: rene@hamel (Rene Mueller)
  7. Organization: Free University of Berlin, Germany
  8. References:  <92314.134711IA8@psuvm.psu.edu>
  9. Date: Thu, 19 Nov 1992 17:45:23 GMT
  10. Lines: 26
  11.  
  12. In article <92314.134711IA8@psuvm.psu.edu>, IA8@psuvm.psu.edu (Alan R. Heyd) writes:
  13. |> Is there a C routine that will round off floating point numbers
  14. |> to the nearest integer.  K&R says that floats are converted to
  15. |> integers by removing the fractional part, however, I could not
  16. |> find mention of a routine which rounds to the nearest integer.
  17. |> What I do now is add 0.5 to the float before it is converted to
  18. |> an integer, for example:
  19. |> 
  20. |>           int i;
  21. |>                                  /* Using Turbo C on and IBM PC: */
  22. |>           i = 6.0 / 0.3;         /*   i = 19                     */
  23. |>           i = 6.0 / 0.3 + 0.5;   /*   i = 20 as expected         */
  24. |> 
  25. |> is there a better way?
  26. |> 
  27. |> Thanks,
  28. |> 
  29. |> Alan Heyd
  30. |> ia8@psuvm.psu.edu
  31. Special roundfunctions, if you have any within your libraries, do it the same way.
  32. Cause it is the fastest solution to ROUND...
  33. But converting to integer just means: forget all behind the decimalpoint....
  34. So i = 6.0 / 0.3; is 'real' converting to integer and will produce on every
  35. machine with any compiler i = 19!
  36.  
  37.     Rene
  38.