From owner-freebsd-questions Tue Mar 2 19:29:14 1999 Delivered-To: freebsd-questions@freebsd.org Received: from cc942873-a.ewndsr1.nj.home.com (cc942873-a.ewndsr1.nj.home.com [24.2.89.207]) by hub.freebsd.org (Postfix) with ESMTP id 5676814E50 for ; Tue, 2 Mar 1999 19:28:32 -0800 (PST) (envelope-from cjc@cc942873-a.ewndsr1.nj.home.com) Received: (from cjc@localhost) by cc942873-a.ewndsr1.nj.home.com (8.9.3/8.8.8) id WAA03732; Tue, 2 Mar 1999 22:35:54 -0500 (EST) (envelope-from cjc) From: "Crist J. Clark" Message-Id: <199903030335.WAA03732@cc942873-a.ewndsr1.nj.home.com> Subject: Re: FP Math In-Reply-To: <19990303022902.23520.qmail@alpha.comkey.com.au> from Greg Black at "Mar 3, 99 12:29:02 pm" To: gjb@comkey.com.au (Greg Black) Date: Tue, 2 Mar 1999 22:35:53 -0500 (EST) Cc: cjclark@home.com, freebsd-questions@FreeBSD.ORG Reply-To: cjclark@home.com X-Mailer: ELM [version 2.4ME+ PL40 (25)] MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG Greg Black wrote, > > Example: Here are two quick C program that I think show why I am > > confused. The first produces an exception and core dumps (for me > > anyway). In this one, I have set floats to values outside of their > > range. It dumps at the point marked, > > This question has nothing at all to do with FreeBSD (or any OS) > and everything to do with a failure to understand the rules of > the C language, so I'll keep this very short. I asked on FreeBSD because I do not get these type of errors when I run on one of my IRIXes. > > #include > > > > int main() > > { > > float a = -1.0e-41; > > At this point your program has entered the domain of undefined > behaviour and from here on the compiler is free to do absolutely > anything it likes, including generating the code represented by > the following fragment: > > execl("/bin/rm", "rm", "-rf", "/", 0); > > If you assign a double constant to a float and that constant is > outside the range of the float (as defined precisely in the > header ), then the behaviour is undefined. OK, your sarcasm aside, now that you mention it, I see how assigning a constant outside of the float range is different than running into overflows during processing. > This also means that the program is allowed to appear to get > some things right, but gives you no right to expect that. As > for why anybody would deliberately do something like this, given > its complete uselessness, that's another question. To simulate where a more complex and useful program is failing. > In short, the answer is: "you can't do that." I won't. > > Could someone explain this? > > Done. OK, but why does this give me a floating point exception, #include int main() { double a = 1.0e41; float b; b = (float)a; printf("%f\n",b); return 0; } This time I skipped any problems of assigning a out-of-bounds value in a constant expression, but get the same result. This is the question I intended to ask. > Anybody who wants or needs further illumination should either > get and read the C Standard or visit comp.lang.c (if it still > exists). I have found no further illumination in my K&R. As far as I can tell from it, all of the above should give defined results. I was under the impression a cast would produce the under/overflow conditions outlined in 'man math.' If my motivation is of any help, I have computational programs that I sometimes run on my FreeBSD machine and sometimes on IRIX. The main reason I sometimes resort to the IRIX is because I do not get the pesky FP exceptions there. Same source code, same input, same compiler (gcc), but different results. My best guess is the math libraries, and I am trying to figure out where the difference is and how to fix it. For example, the above program returns, inf On my SGI. -- Crist J. Clark cjclark@home.com To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message