Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!rochester!cornell!batcomputer!braner From: braner@batcomputer.TN.CORNELL.EDU (braner) Newsgroups: net.micro.amiga,net.micro.atari16 Subject: Re: C compiler bugs Message-ID: <737@batcomputer.TN.CORNELL.EDU> Date: Sat, 26-Jul-86 21:39:30 EDT Article-I.D.: batcompu.737 Posted: Sat Jul 26 21:39:30 1986 Date-Received: Sun, 27-Jul-86 06:08:20 EDT References: <888@ucbcad.BERKELEY.EDU> <678@batcomputer.TN.CORNELL.EDU> Reply-To: braner@batcomputer.UUCP (braner) Organization: Theory Center, Cornell University, Ithaca NY Lines: 92 Xref: mnetor net.micro.amiga:4014 net.micro.atari16:1407 [] The compiler bugs (especially in the floating-point package) keep wasting my time... - here are some tidbits that might save YOU some time: The Megamax library does not have a fabs() function, and abs() does not always work right for FP. So I added this to math.h: #define fabs(x) (((x)<0.0)?(-(x)):(x)) The transcendental functions do NOT warn you if the argument passed to them is illegal. You get GARBAGE (and you don't even know it!), or it crashes, or (in the case of log(0)) it HANGS! I now use LOG() and SQRT() instead of log() and sqrt(). The following functions were also added to math.h: double LOG(x) double x; { if (x <= 0.0) { fprintf(stderr,"illegal arg for log()!\n"); exit(0); } x = log(x); return (x); } double SQRT(x) double x; { if (x < 0.0) { fprintf(stderr,"illegal arg for sqrt()!\n"); exit(0); } x = sqrt(x); return (x); } If your program gets into an infinite loop, or just a loop that's too long, there is no way to stop it unless it's doing I/O. To enable that, add the statement "keybrk();" here and there in your loops, and #include the following file at the top of your source: /* Check if a key has been pressed, and if it was * a control-C print message and exit */ #include keybrk() { if (Cconis()) { /* any key pressed? */ if ((Crawcin()&0x7F) == 0x03) { /* is it ctrl-C? */ fputs("\nbreak!\n\n", stderr); exit(0); } } } Finally, the simple random-number generator, defined as a macro in stdio.h: extern long _seed; #define srand(x) _seed = x; #define rand() (int)(_seed * 6907 + 130253) is no good because a call to rand() does not update _seed, so you keep getting the same nonrandom number again and again! Change to: #define rand() (int)(_seed = (_seed * 6907 + 130253)) Don't forget to do an srand(SEED), with SEED some integer, or else you'll get the same sequence each time you run the program. For an unpredictable sequence (good in games, for example) use the current (encoded!) time as SEED! If you want a floating-point version, returning a number between 0 & 1: #define RND ((double)(rand())/65536.0 + 0.5) (But I would NOT recommend this kind of random number generator for serious statistical purposes!) ------------------------------------------------------------------ - Moshe Braner Corson Hall, Cornell University, Ithaca NY 14853 (607) 272-3487 braner@amvax.tn.cornell.edu (ARPANET) braner%amvax.tn.cornell.edu@WISCVM.BITNET (Bitnet) {decvax,ihnp4,cmcl2,vax135}!cornell!amvax!braner (USENET) ------------------------------------------------------------------