X-Authentication-Warning: delorie.com: mail set sender to djgpp-bounces using -f Message-ID: <4035C9BA.9776D38E@acm.org> From: Eric Sosman X-Mailer: Mozilla 4.72 [en] (Win95; U) X-Accept-Language: en MIME-Version: 1.0 Newsgroups: comp.os.msdos.djgpp Subject: Re: Cross Platform Incompatabilites? - code fragments References: <3 DOT 0 DOT 1 DOT 16 DOT 20040216231142 DOT 38e7a7cc AT earthlink DOT net> <1t3a30d4ia5mq2vqnqqofhopqkqreafpu4 AT 4ax DOT com> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Lines: 42 Date: Fri, 20 Feb 2004 13:45:35 GMT NNTP-Posting-Host: 12.76.163.125 X-Complaints-To: abuse AT worldnet DOT att DOT net X-Trace: bgtnsc05-news.ops.worldnet.att.net 1077284735 12.76.163.125 (Fri, 20 Feb 2004 13:45:35 GMT) NNTP-Posting-Date: Fri, 20 Feb 2004 13:45:35 GMT Organization: AT&T Worldnet To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com Brian Inglis wrote: > > In general, most programmers should not use 32 bit float for > arithmetic, unless they do careful analysis and coding of algorithms > to avoid the *SEVERE* loss of precision that can occur in just a > single operation with unfortunate values. > > A simple example of this would be when you do accumulation in float: > you need to sort and add up the numbers from smallest to largest > absolute value to avoid losing the effect of small values totally. Alternatively, look up Kahan's summation algorithm. It's distressingly simple to demonstrate the effect Brian mentions. This tiny program will do it: #include int main(void) { float sum, add; for (add = 1.0; add < 1e24f; add *= 1024.0f) { sum = 1.0 + add; printf ("1.0 +- %g =? %g\n", add, sum - add); } return 0; } Changing the per-step multiplier from 1024 to 1000 will demonstrate yet another (perhaps even more distressing) problem with limited- precision arithmetic. Switching from `float' to `double' or to `long double' (on platforms where that makes a difference) doesn't really "solve" the problem, but it postpones its onset until the numbers become even more disparate in their magnitudes. Look for the paper "What Every Computer Scientist Should Know About Floating-Point Arithmetic," which is widely available on the Web. -- Eric Sosman esosman AT acm DOT org