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

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!uwm.edu!linac!att!att!dptg!ulysses!allegra!princeton!csservices!kastle!blume
  3. From: blume@kastle.Princeton.EDU (Matthias Blume)
  4. Subject: Re: Rounding floating point numbers in C
  5. Message-ID: <1992Nov21.155741.26569@csservices.Princeton.EDU>
  6. Sender: news@csservices.Princeton.EDU (USENET News System)
  7. Reply-To: blume@kastle.Princeton.EDU (Matthias Blume)
  8. Organization: Dept. of Computer Science, Princeton University
  9. References:  <1992Nov20.215133.3271@enterprise.rdd.lmsc.lockheed.com>
  10. Date: Sat, 21 Nov 1992 15:57:41 GMT
  11. Lines: 57
  12.  
  13. In article <1992Nov20.215133.3271@enterprise.rdd.lmsc.lockheed.com>,
  14. gumerman@aspen.ops.lmsc.lockheed.com () writes:
  15. |> 
  16. |> Hi everybody,
  17. |> 
  18. |> Below is an example program and its output.
  19. |> 
  20. |> 
  21.   [ parts deleted ]
  22.  
  23. |> main()
  24. |> {                                              
  25. |>    one = (float) 1.15;   
  26. |>    two = (float) 1.25;
  27. |>    three = (float) 1.35;
  28. |>    four = (float) 1.45;
  29. |>    five = (float) 1.55;
  30. |>    six = (float) 1.65;
  31. |>    seven = (float) 1.75;
  32. |>    eight = (float) 1.85;
  33. |>    nine = (float) 1.95;
  34. |> 
  35. |>    printf("1.15       %1.1f\n",one);
  36. |>    printf("1.25       %1.1f\n",two);
  37. |>    printf("1.35       %1.1f\n",three);
  38. |>    printf("1.45       %1.1f\n",four);
  39. |>    printf("1.55       %1.1f\n",five);
  40. |>    printf("1.65       %1.1f\n",six);
  41. |>    printf("1.75       %1.1f\n",seven);
  42. |>    printf("1.85       %1.1f\n",eight);
  43. |>    printf("1.95       %1.1f\n",nine);
  44. |> 
  45. |>  }
  46. |> 
  47. |> 
  48.  
  49. Try to print these numbers with a format that uses many more digits.
  50. You will see, that not every number, which has been written in decimal can
  51. be represented precisely in a finite number of binary digits.
  52. Sometimes there is simply no tie in cases where you expect that printf uses
  53. something like ``round to even''.
  54.  
  55. BTW: The same program gives other (also virtually inconsistent) results on a
  56. DECstation (MIPS/Ultrix):
  57.  
  58. 1.15       1.1
  59. 1.25       1.2
  60. 1.35       1.4
  61. 1.45       1.5
  62. 1.55       1.5
  63. 1.65       1.6
  64. 1.75       1.8
  65. 1.85       1.9
  66. 1.95       2.0
  67.  
  68.  
  69. -Matthias
  70.