"Illegal Function Call," Negative Number to Fractional Power (67887)



The information in this article applies to:

  • Microsoft QuickBASIC Compiler for the Apple Macintosh 1.0
  • Microsoft BASIC Compiler, when used with:
    • the operating system: Mac OS (all versions)
  • Microsoft BASIC Interpreter for Apple Macintosh 1.0
  • Microsoft BASIC Interpreter for Apple Macintosh 1.01
  • Microsoft BASIC Interpreter for Apple Macintosh 2.0
  • Microsoft BASIC Interpreter for Apple Macintosh 2.1
  • Microsoft BASIC Interpreter for Apple Macintosh 3.0
  • Microsoft QuickBASIC 4.0
  • Microsoft QuickBASIC 4.0b
  • Microsoft QuickBASIC 4.5
  • Microsoft QuickBASIC for MS-DOS 5.0
  • Microsoft BASIC Compiler for MS-DOS and OS/2 6.0
  • Microsoft BASIC Compiler for MS-DOS and OS/2 6.0b
  • Microsoft Basic Professional Development System (PDS) for MS-DOS and MS OS/2 7.0
  • Microsoft Basic Professional Development System (PDS) for MS-DOS and MS OS/2 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 "Illegal function call" 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 "Illegal function call." 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 "Illegal function call"
PRINT -5 ^ (-1 / 6)      ' same as PRINT -(5 ^ (-1 / 6)); no error
PRINT (-5) ^ (-1 / 6)    ' generates "Illegal function call"
				
To avoid the "Illegal function call" 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 "Warning: the resulting root is an imaginary number. This"
      PRINT "root should be multiplied by i, the square root of -1."
   END IF
				

Modification Type:MinorLast Reviewed:8/16/2005
Keywords:KB67887