From -8498839956166725157
X-Google-Thread: f78e5,eb811809d4f61b12,start
X-Google-Attributes: gidf78e5,public
X-Google-Language: ENGLISH,ASCII
Path: g2news2.google.com!news3.google.com!news4.google.com!news.glorb.com!news.alt.net!comp-std-cpp-robomod!not-for-mail
From: "kanze" <kanze@gabi-soft.fr>
Newsgroups: comp.std.c++
Subject: Using extended precision in floating point
Date: Wed, 17 May 2006 11:36:30 CST
Organization: http://groups.google.com
Lines: 67
Approved: Fergus Henderson <fjh@cs.mu.oz.au>, moderator of comp.std.c++
Message-ID: <1147882581.972497.100340@u72g2000cwu.googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: posting.google.com 1147882587 25705 127.0.0.1 (17 May 2006 16:16:27 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Wed, 17 May 2006 16:16:27 +0000 (UTC)
Return-Path: <devnull@stump.algebra.com>
X-Authentication-Warning: mulga.cs.mu.OZ.AU: fjh set sender to devnull@stump.algebra.com using -f
X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov)
X-Original-To: std-c++@mailman.ucar.edu
Delivered-To: std-c++@mailman.ucar.edu
Delivered-To: std-c++@ucar.edu
User-Agent: G2/0.2
X-HTTP-UserAgent: Mozilla/5.0 (X11; U; SunOS sun4u; en-US; rv:1.0.1) Gecko/20020920 Netscape/7.0,gzip(gfe),gzip(gfe)
Complaints-To: groups-abuse@google.com
Injection-Info: u72g2000cwu.googlegroups.com; posting-host=62.160.54.162;
   posting-account=qsfl8gwAAABZGaLp2a7FeTfDkJamzWYW
X-Virus-Scanned: amavisd-new at ucar.edu
X-Virus-Scanned: amavisd-new at cs.mu.OZ.AU
X-MIME-Autoconverted: from quoted-printable to 8bit by mulga.cs.mu.OZ.AU id k4HGGmtF018714
X-Virus-Scanned: amavisd-new at cs.mu.OZ.AU
Xref: g2news2.google.com comp.std.c++:2016

As a result of a recent discussion in the French language
newsgroup, I am now unsure as to when the compiler can use
extra precision in floating point operations.

�5/10 is clear with regards to expressions: "The values of the
floating operands and the results of floading expressions may be
represented in greater precision and range than required by the
type; the types are not changed thereby."  It seems clear enough
here that in an expression like "(a + b) * c", the compiler can
do the entire expression in long double, even if all of the
variables are float -- and even if "a + b", processed as a
float, was equal to a.  However, there is a footnote
(non-normative, I know) to the effect that "The cast and
assignment operators must still perform their specific
conversions as described in 5.4, 5.2.9 and 5.17."  And �5.7/1
says quite clearly that "The result of the assignment operation
is the value stored in the left operant after the assignment has
taken place;"

I would have normally interpreted this to mean that if I write
something like "(a += b) * c", the value multiplied by c must be
the actual value of the float, and not the extended precision
value which the compiler was allowed to use when there was no
assignment.  Even more so, I would have interpreted this to mean
that in a bit of code like:

    float total = 0.0 ;
    for ( size_t i = 0 ; i < size ; ++ i ) {
        total += array[ i ] ;
    }

where array is float*, the compiler must ensure that the results
are exactly the same as those that it would have obtained by
storing the value to total each time in the loop, and rereading
the stored value in the expression.

I'd always been pretty sure of this, but on analysing some code
which wasn't giving the expected results (they were too accurate
for the algorithm being used !), I found that g++ (on a PC) used
extended precision through out the loop, at least when
optimization was requested.  Is this an error in g++, or have I
misunderstood something?

(For those who are familiar with Intel assembler, the body of
the loop is simply:

.L9:
	fadd	DWORD PTR [%ecx+%eax*4]
	inc	%eax
	cmp	%eax, %edx
	jb	.L9

.)

--
James Kanze                                           GABI Software
Conseils en informatique orient�e objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place S�mard, 78210 St.-Cyr-l'�cole, France, +33 (0)1 30 23 00 34


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]



