| delorie.com/archives/browse.cgi | search |
| Xref: | news2.mv.net comp.lang.c:54599 comp.lang.c++:70107 comp.os.msdos.djgpp:690 gnu.g++.help:4446 gnu.gcc.help:5657 |
| From: | wfeldt AT physik DOT uni-leipzig DOT de (Steffen Winterfeldt) |
| 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 |
| Followup-To: | comp.lang.c,comp.lang.c++,gnu.gcc.help,gnu.g++.help,comp.os.msdos.djgpp |
| Date: | 30 Jan 1996 18:21:39 GMT |
| Organization: | Uni Leipzig |
| Lines: | 32 |
| Message-ID: | <4elnjj$er4@server2.rz.uni-leipzig.de> |
| References: | <4ej9lb$mpc AT fu-berlin DOT de> |
| NNTP-Posting-Host: | tph100.physik.uni-leipzig.de |
| To: | djgpp AT delorie DOT com |
| DJ-Gateway: | from newsgroup comp.os.msdos.djgpp |
Axel Thimm (axl AT zedat DOT fu-berlin DOT de) wrote:
: Hello,
: I am getting confused, about how C/C++ manage float binary operations,
: in particular multiplication. The next C++ example gives me surprising
: results:
: *** cut here: begin file t_prec.cc
: #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; }
: [...]
(c - quad(a)) is not zero, because quad's return value is in a floating point
register and so has higher precision than c.
BTW, with higher optimization (say, -O3), even the second column becomes zero.
Steffen
| webmaster | delorie software privacy |
| Copyright © 2019 by DJ Delorie | Updated Jul 2019 |