Date: Wed, 03 Mar 1999 17:06:26 +1100 From: Andrew Johns <A_Johns@TurnAround.com.au> To: cjclark@home.com Cc: Greg Black <gjb@comkey.com.au>, freebsd-questions@FreeBSD.ORG Subject: Re: FP Math Message-ID: <36DCD162.AC62A10E@TurnAround.com.au> References: <199903030335.WAA03732@cc942873-a.ewndsr1.nj.home.com>
next in thread | previous in thread | raw e-mail | index | archive | help
"Crist J. Clark" wrote: > 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. > inf = infinity?? ie: overflow ie: exception if overflow is not trapped => SGI is merely trapping the oveflow. -- Regards | _/\_/\ Andrew Johns BSc (Comp Sci) | / \ TurnAround Solutions Pty Ltd | \_...__/ http://www.turnaround.com.au/ | \/ "The box said 'Requires Windows 95, NT, or better,' so I installed FreeBSD." 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?36DCD162.AC62A10E>