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

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!stanford.edu!kronos.arc.nasa.gov!iscnvx!enterprise!news
  3. From: gumerman@aspen.ops.lmsc.lockheed.com ()
  4. Subject: Rounding floating point numbers in C
  5. Message-ID: <1992Nov20.215133.3271@enterprise.rdd.lmsc.lockheed.com>
  6. Lines: 61
  7. Sender: news@enterprise.rdd.lmsc.lockheed.com
  8. Nntp-Posting-Host: aspen.ops.lmsc.lockheed.com
  9. Reply-To: gumerman@aspen.ops.lmsc.lockheed.com ()
  10. Organization: Lockheed Missles & Space Co.
  11. Date: Fri, 20 Nov 92 21:51:33 GMT
  12.  
  13.  
  14. Hi everybody,
  15.  
  16. I have been writing come C code using VAX VMS C.  Along the way I have run
  17. into a problem with the way the printf function rounds floating point numbers.
  18. The way numbers are rouned is totally inconsistent.  Sometimes numbers are 
  19. rounded up sometimes they are rounded down.  What is going on here?
  20.  
  21.  
  22. Below is an example program and its output.
  23.  
  24. #include "sourcedir:include_file.c"
  25. #include unixio
  26. #include file
  27. float one=0.0,two=0.0,three=0.0,four=0.0,five=0.0,six=0.0,seven=0.0,eight=0.0;
  28. float nine=0.0;
  29.  
  30. main()
  31. {                                              
  32.    one = (float) 1.15;   
  33.    two = (float) 1.25;
  34.    three = (float) 1.35;
  35.    four = (float) 1.45;
  36.    five = (float) 1.55;
  37.    six = (float) 1.65;
  38.    seven = (float) 1.75;
  39.    eight = (float) 1.85;
  40.    nine = (float) 1.95;
  41.  
  42.    printf("1.15       %1.1f\n",one);
  43.    printf("1.25       %1.1f\n",two);
  44.    printf("1.35       %1.1f\n",three);
  45.    printf("1.45       %1.1f\n",four);
  46.    printf("1.55       %1.1f\n",five);
  47.    printf("1.65       %1.1f\n",six);
  48.    printf("1.75       %1.1f\n",seven);
  49.    printf("1.85       %1.1f\n",eight);
  50.    printf("1.95       %1.1f\n",nine);
  51.  
  52.  }
  53.  
  54.  
  55. OUTPUT:
  56.  
  57.  
  58. 1.15      1.1   /* round down */
  59. 1.25      1.3   /* round up  */
  60. 1.35      1.4   /* round up  */
  61. 1.45      1.5   /* round up  */
  62. 1.55      1.5   /* round down */
  63. 1.65      1.6   /* round down */
  64. 1.75      1.8   /* round up  */
  65. 1.85      1.9   /* round up  */
  66. 1.95      2.0   /* round up  */
  67.  
  68. Does anyone know what is going on here?
  69.  
  70. Any help is appreciated,
  71.  
  72. Signed the insane programmer.
  73.  
  74.