www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/02/02/05:48:43

Xref: news2.mv.net comp.lang.c:54752 comp.lang.c++:70293 comp.os.msdos.djgpp:730 gnu.g++.help:4466 gnu.gcc.help:5672
From: Roland Exler <R DOT Exler AT jk DOT uni-linz DOT ac DOT at>
Newsgroups: comp.lang.c,comp.lang.c++,gnu.gcc.help,gnu.g++.help,comp.os.msdos.djgpp
Subject: Re: float != float and floats as return types
Date: 1 Feb 1996 07:40:13 GMT
Organization: Institute for el. Measurement, University of Linz, Austria
Lines: 49
Message-ID: <4epqot$fj1@alijku06.edvz.uni-linz.ac.at>
References: <4ej9lb$mpc AT fu-berlin DOT de>
NNTP-Posting-Host: sensor4.emt.uni-linz.ac.at
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

axl AT zedat DOT fu-berlin DOT de (Axel Thimm) wrote:
>The next C++ example gives me surprising results:
>	#include <iostream.h>
>	#include <iomanip.h>
>	#include <math.h>
>	float quad( float );
>	int main() {
>	  for( int i=0; i<10; ++i ) {
>	    float a, b, c;
>	    a = i/13.123123;
>	    b = a*a;
>	    c = quad(a);
>	    cout << (b - c) << '\t';
>	    cout << (b - a*a) << '\t';
>	    cout << (c - quad(a)) << '\n';
>	  }
>	  return 0;
>	}
>	float quad( float x ) { return x*x; }

>At first I thought all numbers should be zeros. The middle column might
>not be zero, if the compiler calculates in higher precision than what
>the variables are, so an intermediate storage (b) might loose some
>digits, but I cannot understand what happens with c! Both times a
>function is called, that _is_not_ inlined, so in both cases the same
>loss of digits should be observed.
>Any ideas?

Hi Axel,

Maybe the result of quad is returned on the coprocessor-stack, so it's uses 
with the full precicion even if the result is declared as float. (The 
coprocessor does _all_ computations using double or extended dependend on an 
internal flag.) In this case all floating-point results are truncated only if 
_stored_ to a var with less precicion. If you want to see more compile your 
program with gcc -c t_prec -save-temps and take a look on the file t_prec.s 
(the assembler-output for your program).

Roland

+---------------------------------------+---------------------------+
I Roland Exler                          I EMAIL:                    I
I Universitaet Linz                     I R DOT Exler AT jk DOT uni-linz DOT ac DOT at I
I Institut fuer Elektrische Messtechnik I                           I
I Altenbergerstr. 69                    I Phone:                    I
I A-4040 Linz, AUSTRIA                  I + 43 732 2468 9205        I
+---------------------------------------+---------------------------+


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019