Microsoft KB Archive/62830

= Basic 7.1 &quot;Division by Zero&quot; on 386 with 387, (-X)^2 or Other =

Article ID: 62830

Article Last Modified on 10/20/2003

-

APPLIES TO


 * Microsoft BASIC Professional Development System 7.0
 * Microsoft BASIC Professional Development System 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 &quot;Division by zero&quot; 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 &quot;Division by zero&quot; 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:

 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-

  Remove the unnecessary expression &quot;-1 *&quot;, 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-

  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 &quot;Division by Zero&quot; on an 80387, and the different expression X=-W^2 correctly gives no error. See page 731 of the &quot;Microsoft Basic 7.0: Programmer's Guide&quot; for versions 7.0 and 7.1 to find the order of precedence for Basic operators.

Additional query words: BasicCom 7.00 7.10 buglist7.00 buglist7.10 fixlist1.00

Keywords: KB62830

-

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

© Microsoft Corporation. All rights reserved.