PRB: EMPTY() May Incorrectly Return False When Testing for Zero (310998)



The information in this article applies to:

  • Microsoft Visual FoxPro for Windows 3.0
  • Microsoft Visual FoxPro for Windows 3.0b
  • Microsoft Visual FoxPro for Windows 5.0
  • Microsoft Visual FoxPro for Windows 5.0a
  • Microsoft Visual FoxPro for Windows 6.0
  • Microsoft Visual FoxPro for Windows 7.0

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:
#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("EMPTY() = " + IIF(EMPTY(lnValue), "T", "F") + vfpCR + ;
	"lnValue = " + STR(lnValue, 10, 2) + vfpCR + ;
	"EMPTY(ROUND()) = " + IIF(EMPTY(ROUND(lnValue, 2)), "T", "F") + vfpCR + ;
	"Comparison on lnValue = 0 is " + IIF(lnValue = 0, "T", "F"))

				

Modification Type:MajorLast Reviewed:5/12/2003
Keywords:kbCodeSnippet kbnofix kbprb kbXBase KB310998