Microsoft KB Archive/26006

= Microsoft Knowledge Base =

&quot;Illegal Function Call&quot; from EXE Using Coprocessor
Last reviewed: January 12, 1995

Article ID: Q26006

SUMMARY
When compiled with BC (/o and /d options make no difference) and run on a machine equipped with a math coprocessor, the following program generates an &quot;Illegal function call&quot; message at run time.

SCREEN 1           'fails in any graphics mode OPEN &quot;file4&quot; FOR RANDOM AS #4 CLOSE #4 WINDOW (-320, -100)-(320, 100) FOR x = -202 TO 202 STEP 4 aspect = ABS(202 / x) * 5 / 12 IF aspect < 1 THEN radius = 20 / aspect ELSE radius = 20 END IF     y = 40 * SQR(1 - x ^ 2 / 40804) CIRCLE (x, y), radius,, , , aspect CIRCLE (x, -y), radius,, , , aspect NEXT x

MORE INFORMATION
In QuickBasic Version 4.00, if you use the DOS command SET NO87=&quot;none&quot; to turn off the coprocessor, or if you run inside the QuickBasic editor, the program executes successfully. The program also runs properly in QB and QB87 in QuickBasic Version 3.00.

This is not an error in the QuickBasic Version 4.00 software.

The behavior of the above program in Version 4.00 is due to a difference in the way optimization is handled between the editor and BC. The error occurs in the following line:

y = 40 * SQR(1 - x ^ 2 / 40804) When the program is run inside the editor this line is ultimately evaluated as follows:

y = 40 * SQR( 0 ) However, as an EXE, the same line results in an attempt to find the SQR of a negative number; the same number rounded to zero in the editor.

To work around the problem, assign (1 - x^2 / 40804) to an intermediate variable, set the intermediate variable to zero if it is less than zero, and use the intermediate value in the SQR function.