FIX: Floating Point Field Incorrectly Assigned -9.123E+19

ID: Q125465

1.51 1.52 WINDOWS kbprg kbbuglist kbfixlist

The information in this article applies to:

SYMPTOMS

When you use the RFX_Single() function and set a field to null, the field may actually be set to -9.123E+19 rather than null. For example, the following code doesn't set the field to null as expected.

   CYourRecordset rs;
   rs.Open();
   rs.Edit();
   rs.SetFieldNull(&rs.m_floatfield);
   rs.Update();
   rs.Close();

CAUSE

The problem is caused by a bug in the MFC database classes. Specifically, the RFX_Single() function performs an incorrect check to see if a field is null.

Looking at DBFLT.CPP in the MSVC15\MFC\SRC directory, you'll see this:

   case CFieldExchange::MarkForUpdate:
        if (value != AFX_RFX_SINGLE_PSEUDO_NULL)
            pFX->m_prs->ClearFieldFlags(nField,
                AFX_SQL_FIELD_FLAG_NULL, pFX->m_nFieldType);
        goto LDefault;

The code doesn't work correctly because AFX_RFX_SINGLE_PSEUDO_NULL is defined in AFXDB.H as this:

   #define AFX_RFX_SINGLE_PSEUDO_NULL (-9.123e19)

It should be defined as this:

   #define AFX_RFX_SINGLE_PSEUDO_NULL (-9.123e19f)

As you can see the code compares a float to a double, so the comparison will never be true.

RESOLUTION

To work around this problem, follow these steps:

1. Copy the RFX_Single() function from DBFLT.CPP into the CRecordset's

   .CPP file and rename the function to something like RFX_Single2.

2. Include the following code before the RFX_Single2() function:

   #undef  AFX_RFX_SINGLE_PSEUDO_NULL
   #define AFX_RFX_SINGLE_PSEUDO_NULL (-9.123e19f)

3. Change the call to RFX_Single() in the CRecordset's DoFieldExchange()
   function to RFX_Single2(), and move the function call out of the
   ClassWizard tagged section. The ClassWizard tagged section is the region
   between:

      //{{AFX_FIELD_MAP(CYourRecordset)

   and:

      //}}AFX_FIELD_MAP

NOTE: the problem does not occur when using RFX_Double, so if you can use a double field type in your database, use RFX_Double to avoid the problem.

STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. This problem was corrected in Microsoft Visual C++, 32-bit Edition, version 2.2.

Additional reference words: 1.51 1.52 2.51 2.52 ODBC numeric number KBCategory: kbprg kbbuglist kbfixlist KBSubcategory: MfcDatabase

Keywords          : kb16bitonly kbDatabase kbMFC kbODBC kbVC kbbuglist kbfixlist
Version           : 1.51 1.52
Platform          : WINDOWS
Solution Type     : kbfix

Last Reviewed: September 21, 1997