[comp.sys.mac] Serious MPW C bug!

mkb@ROVER.RI.CMU.EDU (Mike Blackwell) (01/19/88)

If you're using MPW 2.0 C, beware of floating point operations. I just spent
two hours tracking down the following bug (it took that long, because it was
a complicated program, and I didn't even think to look for such a major
compiler bug). Try this simple program:

#include <stdio.h>

main()
{
    float a, b, c;

    a = 2.0; b = 3.0; c = 0.0;
    c += a * b;
    printf("a = %f, b = %f, a*b = %f, c = %f\n", a, b, a * b, c);
}

Guess what you get...
a = 2.0000, b = 3.0000, a*b = 6.0000, c = 0.0000

In fact, trying to get around this with c = c + a * b; will also give you
the same results! The same holds true for using doubles or comps instead of
floats. Using extendeds does seem to work, however. I still can't believe
this. And this DID work in the beta release of 2.0! Arrrrrggggghhhhhh!!!!!!

While I'm bitching about MPW, quick and dirty programs which just use stdio
for output (like this one) crash instantly under Multifinder. And you'd
think, with the horsepower of a Mac2 on my desk, I could compile, or at
least print, in the background, but nooo.... Most every other application
can take advantage of background printing these days. Humph. And I used to
love MPW.

		Mike Blackwell
		The Robotics Institute, Carnegie-Mellon University
		ARPA: mkb@rover.ri.cmu.edu
		(412) 268-8830

joemac@apple.UUCP (Joe MacDougald) (01/20/88)

In article <674@PT.CS.CMU.EDU> mkb@rover.ri.cmu.edu (Mike Blackwell) writes:
>If you're using MPW 2.0 C, beware of floating point operations. I just spent
>two hours tracking down the following bug (it took that long, because it was
>a complicated program, and I didn't even think to look for such a major
>compiler bug). Try this simple program:
[ source omitted...]

This bug was found and fixed.  The version that contains the fix is available
from APDA as MPW C 2.0.2.

>While I'm bitching about MPW, quick and dirty programs which just use stdio
>for output (like this one) crash instantly under Multifinder. And you'd
>think, with the horsepower of a Mac2 on my desk, I could compile, or at
>least print, in the background, but nooo.... Most every other application
>can take advantage of background printing these days. Humph. And I used to
>love MPW.

The C stanard library function printf assumes the defination of stdout as a
buffer for writing output to.  If you have linked your program as an '-t APPL'
(ie. application, the default) the MPW Shell will suspend itself and
run your "application" under the (Multi)Finder.  Here is your problem.  

This buffer is undefined under MultiFinder as well as Finder.  
Printf "works" (read: prints to the screen through the "Macintosh Interface")
when Finder is running due to support included in StdCLib.o.  This functionality
was added primarily to debug tools under an unstable shell environment.

Printf *can not* work, even with this support, under MultiFinder, due to 
the context shifts which move memory around as well as to disk and back.

If you link your program as '-t MPST' (MPW Shell Tool) then it will function 
as one would expect, writing output to the Worksheet ( which is predefined as
stdin, stdout and stderr), even if MultiFinder is active! 


Thanks for your support and I hope this answers your questions and restores your
faith in MPW.

	joe.


|	UUCP		{decwrl, inhp4, hplabs, sun}!apple!joemac              |
|	Internet	joemac@apple.com				       |
|									       |

Since I'm not in Tech Support:

|  The standard disclaimer applies: "I do not speak for Apple Computer, Inc."  |