Microsoft KB Archive/67887

= &quot;Illegal Function Call,&quot; Negative Number to Fractional Power =

Article ID: 67887

Article Last Modified on 8/16/2005

-

APPLIES TO


 * Microsoft QuickBasic Compiler for Macintosh 1.0
 * Microsoft BASIC Compiler 6.0b, when used with:
 * Apple Macintosh OS X
 * Microsoft BASIC Interpreter 1.0
 * Microsoft BASIC Interpreter 1.01 for Macintosh
 * Microsoft BASIC Interpreter 2.0
 * Microsoft BASIC Interpreter 2.1 for Macintosh
 * Microsoft BASIC Interpreter 3.0 for Macintosh
 * Microsoft QuickBasic 4.0
 * Microsoft QuickBASIC 4.0b
 * Microsoft QuickBasic 4.5 for MS-DOS
 * Microsoft QuickBASIC for MS-DOS 5.0
 * Microsoft BASIC Compiler 6.0
 * Microsoft BASIC Compiler 6.0b
 * Microsoft BASIC Professional Development System 7.0
 * Microsoft BASIC Professional Development System 7.1
 * Microsoft GW-BASIC Interpreter 3.20
 * Microsoft GW-BASIC Interpreter 3.22
 * Microsoft GW-BASIC Interpreter 3.23

-



This article was previously published under Q67887



SUMMARY
Raising a negative number to a fractional power gives an &quot;Illegal function call&quot; in Basic because the result will be a complex number, which is not supported in Basic. (A complex number is of the form x+y*i where x is the real component and y is the imaginary component; i is the square root of -1.)

Note: -5^(-Y) is not the same as X^(-Y) when X = -5 because -5^(-Y) is actually parsed as -(5^(-Y)). In other words, the exponentiation operator (^) has greater precedence than the subtraction (-) operator.

This information applies to most versions of Microsoft Basic, including the following:


 * 1) Microsoft QuickBasic version 1.00 for the Apple Macintosh.
 * 2) Microsoft Basic Compiler version 1.00 for the Apple Macintosh.
 * 3) Microsoft Basic Interpreter versions 1.00, 1.01, 2.00, 2.10, and 3.00 for the Apple Macintosh.
 * 4) Microsoft QuickBasic versions 1.00, 1.01, 1.02, 2.00, 2.01, 3.00, 4.00, 4.00b, 4.50 for MS-DOS.
 * 5) Microsoft Basic Compiler versions 5.35 and 5.36 for MS-DOS.
 * 6) Microsoft Basic Compiler versions 6.00 and 6.00b for MS OS/2 and MS-DOS.
 * 7) Microsoft Basic Professional Development System (PDS) versions 7.00 and 7.10 for MS-DOS and MS OS/2.
 * 8) Microsoft GW-Basic Interpreter versions 3.20, 3.22, 3.23 for MS-DOS.



MORE INFORMATION
The first PRINT statement below gives &quot;Illegal function call.&quot; Many programmers will try debugging this code by substituting an actual constant for X, and the PRINT will seem to give the correct result. However, upon close examination of the precedence of operators, it will be apparent that the exponential symbol takes precedence over the minus sign. The expression 5^(-1/6) is parsed first and then the negation of this expression is performed. The third PRINT demonstrates what is actually happening in the first PRINT statement: X = -5 PRINT X ^ (-1 / 6)      ' generates &quot;Illegal function call&quot; PRINT -5 ^ (-1 / 6)     ' same as PRINT -(5 ^ (-1 / 6)); no error PRINT (-5) ^ (-1 / 6)   ' generates &quot;Illegal function call&quot; To avoid the &quot;Illegal function call&quot; message, raise the absolute value (ABS) of X to the fractional power. If you do this when X is a negative number, please remember that you must multiply by i (the square root of -1) to get the true mathematical result. Because the square root of -1 cannot be represented in Basic, you must keep track of imaginary number results yourself using a flag variable or warning message, for example: X=-5 PRINT ABS(X) ^ (-1 / 6) IF SGN(X) < 0 THEN PRINT &quot;Warning: the resulting root is an imaginary number. This&quot; PRINT &quot;root should be multiplied by i, the square root of -1.&quot; END IF

Additional query words: QuickBas BasicCom

Keywords: KB67887

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.