Menu

#175 wxNumberValidator - 0 greater than 0.001 error

CVS_HEAD
open
None
5
2017-05-10
2017-05-09
No

In fvalnum.cpp, function CompParts(), line 1215, for two numbers having the same sign, the one having the smaller exponent is considered the smaller. This is not true if one of them is 0. Example:

0 = +0e0
0.001 = +1e-3

Exponent -3 is smaller than exponent 0, but 0.001 is greater than 0.

I inserted the following code at line 1215 (before comparing exponents):

bool left = IsZero(sP1);
bool right = IsZero(sP2);

if (left && right)  return (0);         // Both 0
if (left)  return(sP2.sSign ? -1 : 1);  // One is 0, the other not. The other is...
if (right) return(sP1.sSign ? 1 : -1);  // ...smaller/bigger depending in it's sign.

And I added a new function IsZero() as this:

bool wxNumberValidator::IsZero(const stringParts& sP)
{

if (sP.sExp != 0)  return (false);

int     len = sP.sDigits.Len();

for (int i = 0; i < len; i++)  {
    if (sP.sDigits.at(i) != L'0')  return (false);
}

return (true);

}

This way, if both numbers are 0, CompParts() returns 0. And if only one number is 0, the other is smaller or greater depending on it's sign. Negative is smaller than 0, and positive is greater.

Discussion

  • Manuel MS

    Manuel MS - 2017-05-09

    Thank you very much for finding this bug.
    I prefer not to add another function. In this case this is easy as wxFormatStringAsNumber::StringToParts() sets a '0' for sP.sDigits when it's really zero.

    I've fixed the bug. Please download FormatValidator_21.zip from wxCode

     
    • Gilbert Pelletier

      That works well. Thank-you!

      But now (with version 2.1) I have errors in fvalgred.cpp, lines 89, 91, 95 and 97. The arguments of wxGrid::SetCellTextColour() and wxGrid::SetCellBackgroundColour() are respectively: color, row and column. This is deprecated. The new version needs: row, column and color.

      I have wxWidgets 3.1 compiled with WXWIN_COMPATIBILITY_2_8 and WXWIN_COMPATIBILITY_3_0 defined to 0.

      The version 2.0 of fvalgred.cpp used the new way (row, column, color) and compiled correctly. So maybe you did the change to be compatible with older versions of wxWidgets? If this is the case it would be better to use conditional compilation I guess (depending on wxWidget's version).

      Have a good day !

       
  • Manuel MS

    Manuel MS - 2017-05-10

    Yes, those lines in fvalgred need changes for wx3.1. And also some sizers flags in the example.
    I'll fix them soon.

     
    • Manuel MS

      Manuel MS - 2017-05-10

      wxCode components page states that wxFormatValidator needs wx2.9. The fvalgred changes were a regression. The sizers issue comes from wx3.0.

      Now fixed and updated (FormatValidator_22).

       

      Last edit: Manuel MS 2017-05-10
      • Gilbert Pelletier

        It is ok now. Good work Manuel !

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.