-2

I have version number 1.1, 1.2, 1.3 and I need to check version less or greater than 1.2, but while debugging I get wrong answer than expected

    float versionNumber = versinInfo.toFloat();
    static float const VERSION_NUMBER(1.2);
    if(abs((versionNumber - VERSION_NUMBER) <= 0.001))
    {
      // do operation
    }

versionNumber comes 1.10000005, I though to change check from 0.001 to 0.0000005 but it maynt be a correct fix

please suggest best method

user2672165
  • 2,986
  • 19
  • 27
Sijith
  • 3,740
  • 17
  • 61
  • 101

2 Answers2

2

Version numbers are inherently integers. You already have a class, this class should have an integer value for the major version, and integer version for the minor version, and comparison operators (that's what you are trying to do).

Other schemes even have a third patch integer, a string for alpha/RC... Implement this properly in a class that uses proper semantics (i.e. a method named is_one_minor_version_away where you properly test is there is only one minor version change).

Also what happens if it's 2.9 against 3.0 in your case?

Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
1

Why don't you create class that hold major version and minor version of passed versionInfo and add logic that checks there. The idea would be not to parse this as float, but to separate it by comma and take that info as int.