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

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.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: <1992Nov22.162704.6694@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> <7959@charon.cwi.nl> <1992Nov22.040145.6371@u.washington.edu>
  10. Date: Sun, 22 Nov 1992 16:27:04 GMT
  11. Lines: 49
  12.  
  13. In article <1992Nov22.040145.6371@u.washington.edu>,
  14. golbone@stein.u.washington.edu (Sima Hashemifar) writes:
  15.  
  16. [quote deleted]
  17.  
  18. |> 
  19. |> No! No! No! You can not in any way deduce the properties of the floating
  20. |> point system based on the rounding behavior. For one, most modern fp
  21. |> units may be convinced to act in many way, affine ...
  22. |> More impotantly, there are elementary rules for rounding in scientific
  23. |> world. Any odd number followed by 5 is rounded down while even numbers
  24.           ^^^^^^^^^^              ^^^^
  25. I believe you mixed something here... I know only ``round to even''.
  26.  
  27. |> are rounded up. Based on this simple rule, the results printed above
  28. |> are not valid in your laboratory notebook.
  29. |> Thanks.
  30. |> BTW, I ran the numbers through my fortran compiler on DEC/Ultrix, I got 
  31. |> the scientifically correct results, BUT the C compiler generated the
  32. |> results above. I can only say that "C" has a different floating point
  33. |> model than Fortran.
  34.  
  35. Ok, so much for the Fortran vs. C thread... :(
  36.  
  37. I don't know, what the rounding rule has to do with the original question.
  38. The fact is, that numbers like '1.15' cannot be accurately represented in an
  39. IEEE-like binary representation.  Any sane fp model would choose the
  40. representation that is CLOSEST IN VALUE.  This might be bigger or smaller
  41. depending on the original number.  It has nothing to do with the digit at the
  42. end.  Ties are to be broken, if there are two internal representations which
  43. both are closest in value.
  44.     Similarly, when you print the number with a limited number of digits,
  45. then you should at least choose the EXTERNAL representation, that is closest
  46. to the internal number.  Again, ties are to be broken whenever there is more
  47. than one possible representation.  The disadvantage of this approach is, that
  48. what you print is not necessarely what you've read, even if you are allowed to
  49. use as many digits as the original number in your input had.  (Think about it!)
  50. A better approach is to print something which is guaranteed to reproduce
  51. EXACTLY THE SAME INTERNAL REPRESENTATION when read back.
  52.  
  53. Two excellent papers about these number converting issues are:
  54.     William D. Clinger: ``How to Read Floating Point Numbers Accurately''
  55. and
  56.     Guy L. Steele Jr. and Jon L White: ``How to Print Floating-Point
  57.     Numbers Accurately''
  58. both to be found in ``Proceedings of the ACM SIGPLAN '90 Conference on
  59. Programming Language Design and Implementation''
  60.  
  61. -Matthias
  62.