From: dontmailme AT iname DOT com (Steamer) Newsgroups: comp.os.msdos.djgpp Subject: Re: Possible truncation error Date: Tue, 27 Jun 2000 19:09:37 GMT Organization: always disorganized Lines: 40 Message-ID: <3958fbdb.42786311@news.freeserve.net> References: <_x665.15310$Za1 DOT 242989 AT newsc DOT telia DOT net> NNTP-Posting-Host: modem-163.cleaner-wrasse.dialup.pol.co.uk X-Trace: newsg4.svr.pol.co.uk 962132978 13907 62.136.246.163 (27 Jun 2000 19:09:38 GMT) NNTP-Posting-Date: 27 Jun 2000 19:09:38 GMT X-Complaints-To: abuse AT theplanet DOT net X-Newsreader: Forte Free Agent 1.11/32.235 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com Erik Berglund wrote: > Hi, I tried the following program, which gives > j=9999. I think the right result should be 10000, > because a=0x40c3880000000000, which is exactly 10000. > > #include > #include > int main() { > double a; > int j,*p; > > a = pow(100,2); Although 'a' is a double, it is held for the time being in the FPU, which is at 80-bit precision. It's value is 0x400C9C3FFFFFFFFFFFFE, which is just less than 10000. > j = a; This therefore sets j to 9999. > p = (int*)&a; > printf("%08x%08x\n",*(p+1),*p); > printf("%i\n",j); > return(0); > } > > If I insert a printf("\n") before j=a;, the result > becomes 10000 instead, and I have no idea why. This is because gcc couldn't leave the double in the FPU during the function call, so it stores it in memory, converting to 64-bit in the process (because it's a double). This happens to make it 0x40c3880000000000, which (as you pointed out) is exactly 10000. Don't you just love floating point arithmetic? S.