home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / c / 16245 < prev    next >
Encoding:
Internet Message Format  |  1992-11-09  |  1.5 KB

  1. Path: sparky!uunet!caen!uwm.edu!csd4.csd.uwm.edu!eli
  2. From: eli@csd4.csd.uwm.edu (Elihu Lubkin)
  3. Newsgroups: comp.lang.c
  4. Subject: Rounding floats to integers
  5. Date: 10 Nov 1992 01:28:24 GMT
  6. Organization: University of Wisconsin - Milwaukee
  7. Lines: 35
  8. Message-ID: <1dn37oINNr42@uwm.edu>
  9. NNTP-Posting-Host: 129.89.7.4
  10.  
  11. From uwm.edu!biosci!agate!usenet.ins.cwru.edu!gatech!rutgers!psuvax1!psuvm!ia8 Mon Nov  9 18:47:13 1992
  12. Path: uwm.edu!biosci!agate!usenet.ins.cwru.edu!gatech!rutgers!psuvax1!psuvm!ia8
  13. From: IA8@psuvm.psu.edu (Alan R. Heyd)
  14. Newsgroups: comp.lang.c
  15. Subject: Rounding floats to integers
  16. Message-ID: <92314.134711IA8@psuvm.psu.edu>
  17. Date: 9 Nov 92 18:47:11 GMT
  18. Organization: Penn State University
  19. Lines: 18
  20.  
  21. >Is there a C routine that will round off floating point numbers
  22. >to the nearest integer.  K&R says that floats are converted to
  23. >integers by removing the fractional part, however, I could not
  24. >find mention of a routine which rounds to the nearest integer.
  25. >What I do now is add 0.5 to the float before it is converted to
  26. >an integer, for example:
  27. >
  28. >          int i;
  29. >                                 /* Using Turbo C on and IBM PC: */
  30. >          i = 6.0 / 0.3;         /*   i = 19                     */
  31. >          i = 6.0 / 0.3 + 0.5;   /*   i = 20 as expected         */
  32. >
  33.  
  34.       But if i = -6.0 / 0.3 + 0.5 then i = -19 so you also have to
  35. watch out for the sign.
  36.                           Thelma Lubkin
  37.  
  38. >is there a better way?
  39. >
  40. >Thanks,
  41. >
  42. >Alan Heyd
  43. >ia8@psuvm.psu.edu
  44.  
  45.  
  46.