home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!att!dptg!ulysses!allegra!princeton!csservices!kastle!blume
- From: blume@kastle.Princeton.EDU (Matthias Blume)
- Subject: Re: Rounding floating point numbers in C
- Message-ID: <1992Nov22.162704.6694@csservices.Princeton.EDU>
- Sender: news@csservices.Princeton.EDU (USENET News System)
- Reply-To: blume@kastle.Princeton.EDU (Matthias Blume)
- Organization: Dept. of Computer Science, Princeton University
- References: <1992Nov20.215133.3271@enterprise.rdd.lmsc.lockheed.com> <7959@charon.cwi.nl> <1992Nov22.040145.6371@u.washington.edu>
- Date: Sun, 22 Nov 1992 16:27:04 GMT
- Lines: 49
-
- In article <1992Nov22.040145.6371@u.washington.edu>,
- golbone@stein.u.washington.edu (Sima Hashemifar) writes:
-
- [quote deleted]
-
- |>
- |> No! No! No! You can not in any way deduce the properties of the floating
- |> point system based on the rounding behavior. For one, most modern fp
- |> units may be convinced to act in many way, affine ...
- |> More impotantly, there are elementary rules for rounding in scientific
- |> world. Any odd number followed by 5 is rounded down while even numbers
- ^^^^^^^^^^ ^^^^
- I believe you mixed something here... I know only ``round to even''.
-
- |> are rounded up. Based on this simple rule, the results printed above
- |> are not valid in your laboratory notebook.
- |> Thanks.
- |> BTW, I ran the numbers through my fortran compiler on DEC/Ultrix, I got
- |> the scientifically correct results, BUT the C compiler generated the
- |> results above. I can only say that "C" has a different floating point
- |> model than Fortran.
-
- Ok, so much for the Fortran vs. C thread... :(
-
- I don't know, what the rounding rule has to do with the original question.
- The fact is, that numbers like '1.15' cannot be accurately represented in an
- IEEE-like binary representation. Any sane fp model would choose the
- representation that is CLOSEST IN VALUE. This might be bigger or smaller
- depending on the original number. It has nothing to do with the digit at the
- end. Ties are to be broken, if there are two internal representations which
- both are closest in value.
- Similarly, when you print the number with a limited number of digits,
- then you should at least choose the EXTERNAL representation, that is closest
- to the internal number. Again, ties are to be broken whenever there is more
- than one possible representation. The disadvantage of this approach is, that
- what you print is not necessarely what you've read, even if you are allowed to
- use as many digits as the original number in your input had. (Think about it!)
- A better approach is to print something which is guaranteed to reproduce
- EXACTLY THE SAME INTERNAL REPRESENTATION when read back.
-
- Two excellent papers about these number converting issues are:
- William D. Clinger: ``How to Read Floating Point Numbers Accurately''
- and
- Guy L. Steele Jr. and Jon L White: ``How to Print Floating-Point
- Numbers Accurately''
- both to be found in ``Proceedings of the ACM SIGPLAN '90 Conference on
- Programming Language Design and Implementation''
-
- -Matthias
-