关于C / C比较整数和浮点/双精度++ [英] About comparing an integer and a float/double in C/C++

查看:174
本文介绍了关于C / C比较整数和浮点/双精度++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面这段code(随意改变浮动双)的:

  A级
{
    上市:
        无效setValueFloat(浮点V){
              m_floatValue = V / 3.6; //米/秒和所述 - →; km / h的转换
        }
        无效setValueInt(INT V1,V2 INT){
              m_intValue1 = V1; m_intValue2 = V2;
        }        布尔conditionIsOk()
        {
             如果(m_intValue1> m_intValue2)
             {
                  如果(m_intValue1 - m_intValue2> m_floatValue)
                  {
                      返回true;
                  }
             }
             返回false;
        }    私人的:
        INT m_intValue1,m_intValue2;
        浮m_floatValue;};

和其他地方:

  A一;
INT东西= 5; //故意INT
INT somethingElse = 6; //这些都只是一些数字,而不是生产数据!
INT moreStuff = 7;a.setValueFloat(东西);
a.setValueInt(somethingElse,moreStuff);
如果(a.conditionIsOk())
{
   // yippee的!
}

和问题:


  1. 如何安全是它的算术运算的结果,我们比较的 INT 浮动上面给出的情况呢?


  2. 是否有必要(浮点)m_intValue1 - (浮点)m_intValue2> m_floatValue 对于这种情况呢?


  3. 凡在C / C ++标准我可以找到一个究竟这种局面线?


  4. 默认情况下会做什么类型转换为简单的情况 m_intValue1 - m_intValue2> m_floatValue ? (我怎么能证明这点的方式,他也看到它(视觉),只是认为它工作是不够的:)别人)



解决方案

  1. 这取决于实际的执行(即它的编译器和架构使用)。在有32位 INT 和IEEE754 binary32 浮动取值整数可以重新$ P $典型的系统psented正是达2 ^ 24的花车,所以不为各种可能的值。所以,不,它不是一般的安全,但可能是安全的,如果你的整数的使用范围(或在这种情况下,相当的差别!)和彩车进行适当的限制。


  2. 没有!事实上 m_intValue1 - m_intValue2> m_floatValue 是浮动的差异的计算后,会发生转换好(见注有关上述点差)。你可以明确的写的static_cast<浮动>(m_intValue1 - m_intValue2)GT; m_floatValue ,但这不是必要的。


  3. 转换覆盖在C ++标准第4章(见的草案N3242 )。特别是4.9浮整体转换,也注意到5§10通常的算术转换,这也适用于比较。因为这个问题还标有C,C标准(见的 darft N1570 )的相应部分是6.3.1,特别是6.3.1.4和6.3.1.8。


  4. 请参阅答案为2和3。


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 :) )

解决方案

  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.

这篇关于关于C / C比较整数和浮点/双精度++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆