About comparing an integer and a float/double in C/C++

Ferenc Deak

I have the following piece of code (feel free to change the float with double):

class A
{
    public:
        void setValueFloat(float v) {
              m_floatValue = v / 3.6;  // m/s <-> km/h conversion
        }
        void setValueInt(int v1, int v2) { 
              m_intValue1 = v1; m_intValue2 = v2;
        }

        bool conditionIsOk()
        {
             if(m_intValue1 > m_intValue2)
             {
                  if(m_intValue1 - m_intValue2 > m_floatValue)
                  {
                      return true;
                  }
             }
             return false;
        }

    private:
        int m_intValue1, m_intValue2;
        float m_floatValue;

};

and somewhere else:

A a;
int something = 5; // Intentionally int
int somethingElse = 6; //these are just some numbers, not production data!!!
int moreStuff = 7;

a.setValueFloat(something);
a.setValueInt(somethingElse, moreStuff);
if(a.conditionIsOk())
{
   // Yippee!
}

And the questions:

  1. How safe is it to compare the result of an arithmetic operation on ints to a float given the situation above?

  2. Is it necessary to (float)m_intValue1 - (float)m_intValue2 > m_floatValue for this situation?

  3. Where in the C / C++ standard can I find a line about exactly this situation?

  4. What typecasts will be done by default for the simple situation m_intValue1 - m_intValue2 > m_floatValue ? (and how can I show this to someone else in a way that he also sees it (visually), "just believing that it works" is not enough :) )

Joe
  1. This depends on the actual implementation (i.e. which compiler and which architecture are used). On typical systems with 32 bit ints and IEEE754 binary32 floats integers can be represented exactly up to +-2^24 as floats, so not for the full range of possible values. So no, it is not safe in general, but may be safe if the used range of your integers (or in this case rather the difference!) and floats is limited appropriately.

  2. No! In fact m_intValue1 - m_intValue2 > m_floatValue is better as the conversion to float happens after the computation of the difference (see note about difference in the above point). You can be explicit and write static_cast<float>(m_intValue1 - m_intValue2) > m_floatValue, but this is not necessary.

  3. Conversions are covered in chapter 4 of the C++ standard (see draft N3242). In particular 4.9 Floating-integral conversions, also note 5§10 "usual arithmetic conversions" which also applies to comparisons. As the question is also tagged with C, in the C standard (see darft N1570) the corresponding section is 6.3.1 and in particular 6.3.1.4 and 6.3.1.8.

  4. See answers to 2. and 3.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Comparing List<Integer> by index

From Dev

Comparing an int and Integer in Java

From Dev

Comparing String with Integer in javascript

From Dev

Comparing A String To Integer, Not Working?

From Dev

Comparing char with integer

From Dev

Comparing integer with array of integers

From Dev

std::pow with integer parameters, comparing to an integer type

From Dev

Integer not comparing equal to its value

From Dev

Comparing Long to simple integer in Java

From Dev

comparing integer value in if statement in bash

From Dev

Comparing BinarySearchTree nodes of type Integer

From Dev

Comparing two Integer Wrapper classes

From Dev

Is comparing signed and unsigned integer safe?

From Dev

Comparing an integer variable in the Bash shell

From Dev

Comparing an integer amongst other integers

From Dev

Python TypeError: an integer is required comparing times

From Dev

Comparing 2 arrays to output total integer

From Dev

Comparing BOOLEAN TRUE to INTEGER value in PHP

From Dev

similar_text - string / integer comparing

From Dev

Comparing integer in record with another record in MySQL

From Dev

comparing nil to integer in Objective-C

From Dev

On comparing different way of reading an integer from a file

From Dev

ERROR: integer expression expected - comparing tables

From Dev

similar_text - string / integer comparing

From Dev

If returning wrong result when comparing integer with string

From Dev

Interesting behavior comparing integer with array in PHP

From Dev

Comparing floats with more than 1 integer

From Dev

Pylint complains about comparing a string to a literal with 'is'

From Dev

Understand about bitwise operator and comparing them

Related Related

HotTag

Archive