Basic 7.1 "Division by Zero" on 386 with 387, (-X)^2 or Other (62830)



The information in this article applies to:

  • 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

This article was previously published under Q62830

SYMPTOMS

Executing the following line of code on an 80386 machine equipped with an 80387 math coprocessor results in a "Division by zero" error:
   X = (-1 * W) ^ 2     ' Note that W defaults to a zero (0) value
				

-or-

   X = (-W) ^ 2     ' Note that W defaults to a zero (0) value
				
This problem has also been reported with other similar numeric expressions where a variable equal to zero (0) is multiplied by a negative value and that expression in parentheses is raised to a power.

The "Division by zero" error occurs in both the QBX.EXE environment and within a compiled .EXE in Microsoft Basic Professional Development System (PDS) versions 7.0 and 7.1. The error occurs only on 80386 computers with 80387 coprocessors.

STATUS

Microsoft has confirmed this to be a bug with Microsoft Basic PDS versions 7.0 and 7.1 for MS-DOS and MS OS/2. We are researching this bug and will post new information here as it becomes available. This problem does not occur with other Microsoft Basic products.

MORE INFORMATION

You can use any of the following methods to work around the problem:

  1. Use the DOS command SET NO87=NONE to disable Basic's use of the 80387 coprocessor. The program will then execute correctly. Note that disabling Basic's use of the coprocessor usually slows program execution. -or-

  2. Remove the unnecessary expression "-1 *", or remove the unnecessary negative sign (negation operator), from the formula. The negative sign is unnecessary because squaring a number (raising a number to the power of 2) always makes a real number positive. The following simpler expression gives no error, and is equivalent to the expression that gives the problem:
          X = W ^ 2
    						
    -or-

  3. Change the line of code into two lines, using a temporary variable to hold the value of (-1 * W), as follows:
          TEMP = (-1 * W)
          X = TEMP ^ 2
    						
NOTE: According to Basic rules of precedence, Basic evaluates the expression X=(-W)^2 in a different order than the different expression X=-W^2, giving a different value for X. The exponentiation operator (^) has a higher precedence than the negation operator (-). The expression X=-W^2 first raises W to the power of 2, then negates the answer, so you always get a negative or zero result for X. The expression X=(-W)^2 first negates W, then raises that value to the power of 2. The expression X=(-W)^2 incorrectly gives "Division by Zero" on an 80387, and the different expression X=-W^2 correctly gives no error. See page 731 of the "Microsoft Basic 7.0: Programmer's Guide" for versions 7.0 and 7.1 to find the order of precedence for Basic operators.

Modification Type:MajorLast Reviewed:10/20/2003
Keywords:KB62830