Date: Tue, 2 Mar 1999 22:35:53 -0500 (EST) From: "Crist J. Clark" <cjc@cc942873-a.ewndsr1.nj.home.com> To: gjb@comkey.com.au (Greg Black) Cc: cjclark@home.com, freebsd-questions@FreeBSD.ORG Subject: Re: FP Math Message-ID: <199903030335.WAA03732@cc942873-a.ewndsr1.nj.home.com> In-Reply-To: <19990303022902.23520.qmail@alpha.comkey.com.au> from Greg Black at "Mar 3, 99 12:29:02 pm"
next in thread | previous in thread | raw e-mail | index | archive | help
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 <stdio.h> > > > > 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 <float.h>), 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 <stdio.h> 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
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199903030335.WAA03732>