Microsoft KB Archive/310998

= PRB: EMPTY May Incorrectly Return False When Testing for Zero =

Article ID: 310998

Article Last Modified on 5/12/2003

-

APPLIES TO


 * Microsoft Visual FoxPro 3.0 Standard Edition
 * Microsoft Visual FoxPro 3.0b Standard Edition
 * Microsoft Visual FoxPro 5.0 Standard Edition
 * Microsoft Visual FoxPro 5.0a
 * Microsoft Visual FoxPro 6.0 Professional Edition
 * Microsoft Visual FoxPro 7.0 Professional Edition

-



This article was previously published under Q310998



SYMPTOMS
If you do floating point math and test the result for zero with the EMPTY function and the equals operator (=), you may see different results.



CAUSE
Floating point math does the computations in binary fractions, which may not convert exactly back to decimal. Rounding errors can cause the internal representation of values to be slightly different than their displayed values.



RESOLUTION
Either test for the value being equal to 0 (because the = operator allows for slight differences) or round the value you are testing to the number of decimal places used in your computations.



MORE INFORMATION
To see this behavior, run the following code:
 * 1) DEFINE vfpCR CHR(13) + CHR(10)

LOCAL lnValue

CREATE CURSOR test (nValue N (10,2)) INSERT INTO test VALUES (260.70) INSERT INTO test VALUES (-31.60) INSERT INTO test VALUES (-260.70) INSERT INTO test VALUES (31.60)

lnValue = 0 SELECT test SCAN lnValue = lnValue + nValue ENDSCAN

=MESSAGEBOX(&quot;EMPTY = &quot; + IIF(EMPTY(lnValue), &quot;T&quot;, &quot;F&quot;) + vfpCR + ;   &quot;lnValue = &quot; + STR(lnValue, 10, 2) + vfpCR + ;    &quot;EMPTY(ROUND) = &quot; + IIF(EMPTY(ROUND(lnValue, 2)), &quot;T&quot;, &quot;F&quot;) + vfpCR + ;    &quot;Comparison on lnValue = 0 is &quot; + IIF(lnValue = 0, &quot;T&quot;, &quot;F&quot;))

Keywords: kbnofix kbxbase kbcodesnippet kbprb KB310998

-

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

© Microsoft Corporation. All rights reserved.