Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: utzoo!mnetor!seismo!ut-sally!ut-ngp!infotel!pollux!ti-csl!herman
From: herman@ti-csl.CSNET (Herman Schuurman)
Newsgroups: comp.lang.c,comp.unix.xenix
Subject: Re: Are the floating point routines on my machine broken?
Message-ID: <10944@ti-csl.CSNET>
Date: Wed, 31-Dec-86 11:36:39 EST
Article-I.D.: ti-csl.10944
Posted: Wed Dec 31 11:36:39 1986
Date-Received: Wed, 31-Dec-86 21:38:20 EST
References: <442@catnip.UUCP>
Organization: TI Computer Science Center, Dallas
Lines: 46
Xref: mnetor comp.lang.c:566 comp.unix.xenix:34

> .... Is roundup considered acceptable
> behavior for floating point routines?  Or am I somehow misinterpreting
> the problem?
> 
> Ben Broder

Your floating point routines seem to be using the standard IEEE double
precision floating point format, with the rounding mode set to
"round to nearest".  This means that they are rounding to the nearest
representable result, which is quite different from rounding up all the
time.  The accumulated rounding error shows up in the final bit of
the result, which makes it 3.000000000000004e0, rather than 3.0.

The big problem is that .2 is not exactly representable in a binary
floating point format.  It therefore always suffers from rounding
errors.  If you printed the results of your example more precisely,
you would come up with the following results:

0.0e0
0.2e0
0.4e0
0.6000000000000001e0
0.8e0
1.0e0
1.2e0
1.4e0
1.5999999999999999e0
1.7999999999999999e0
1.9999999999999999e0
2.1999999999999999e0
2.4e0
2.6e0
2.8000000000000003e0
3.0000000000000004e0

This clearly shows the rounding errors during addition.  Notice that
some of the results are too low, whereas other results are to high,
indicating a "round to nearest", rather than a "round to positive infinity"
(as IEEE calls rounding up).


-- 
Herman Schuurman 	ARPA:  herman%TI-CSL@CSNET-RELAY.ARPA
Texas Instruments Inc.	CSNET: herman@TI-CSL
PO Box 226015 M/S 238	USENET: {ut-sally,convex!smu,texsun,rice}!ti-csl!herman
Dallas, Texas 75266	VOICE: (214) 995-0845