home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!uwm.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: <1992Nov21.155741.26569@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>
- Date: Sat, 21 Nov 1992 15:57:41 GMT
- Lines: 57
-
- In article <1992Nov20.215133.3271@enterprise.rdd.lmsc.lockheed.com>,
- gumerman@aspen.ops.lmsc.lockheed.com () writes:
- |>
- |> Hi everybody,
- |>
- |> Below is an example program and its output.
- |>
- |>
- [ parts deleted ]
-
- |> main()
- |> {
- |> one = (float) 1.15;
- |> two = (float) 1.25;
- |> three = (float) 1.35;
- |> four = (float) 1.45;
- |> five = (float) 1.55;
- |> six = (float) 1.65;
- |> seven = (float) 1.75;
- |> eight = (float) 1.85;
- |> nine = (float) 1.95;
- |>
- |> printf("1.15 %1.1f\n",one);
- |> printf("1.25 %1.1f\n",two);
- |> printf("1.35 %1.1f\n",three);
- |> printf("1.45 %1.1f\n",four);
- |> printf("1.55 %1.1f\n",five);
- |> printf("1.65 %1.1f\n",six);
- |> printf("1.75 %1.1f\n",seven);
- |> printf("1.85 %1.1f\n",eight);
- |> printf("1.95 %1.1f\n",nine);
- |>
- |> }
- |>
- |>
-
- Try to print these numbers with a format that uses many more digits.
- You will see, that not every number, which has been written in decimal can
- be represented precisely in a finite number of binary digits.
- Sometimes there is simply no tie in cases where you expect that printf uses
- something like ``round to even''.
-
- BTW: The same program gives other (also virtually inconsistent) results on a
- DECstation (MIPS/Ultrix):
-
- 1.15 1.1
- 1.25 1.2
- 1.35 1.4
- 1.45 1.5
- 1.55 1.5
- 1.65 1.6
- 1.75 1.8
- 1.85 1.9
- 1.95 2.0
-
-
- -Matthias
-