Qt4 实现qIsInf 和 qIsNaN 函数

标题中的两个函数,在qt4.7版本中没有实现,但是在 Assistant 中索引可以,但是文档中没有介绍,

所以在代码中直接调用的时候,会报错。

 

c++ 也是从11标准之后std 才实现 isinf 和 isnan 这样的功能,

而我使用的开发平台:qt4 vs2008 显然不能期望通过直接使用现有的库函数了

 

在Qt5.6 版本的帮助文档中,这么描述:

bool qIsInf(float f)

Returns true if the float f is equivalent to infinity.

bool qIsNaN(float f)

Returns true if the float f is not a number (NaN).

 

寻思着自己实现,参照qt高版本怎么实现的,

qt5.6 源代码目录位置: C:\Qt\Qt5.6.0\5.6\Src\qtbase\src\corelib\global

三个源文件:

qnumeric.h  //部分代码段

#ifndef QNUMERIC_H
#define QNUMERIC_H

#include <QtCore/qglobal.h>

QT_BEGIN_NAMESPACE


Q_CORE_EXPORT bool qIsInf(double d);
Q_CORE_EXPORT bool qIsNaN(double d);

Q_CORE_EXPORT bool qIsInf(float f);
Q_CORE_EXPORT bool qIsNaN(float f);


QT_END_NAMESPACE

#endif // QNUMERIC_H
qnumeric.cpp 部分代码段

#include "qnumeric.h"
#include "qnumeric_p.h"   //从这里可以判断,qt_is_inf qt_is_nan的实现在这个头文件中
#include <string.h>

QT_BEGIN_NAMESPACE

/*!
    Returns \c true if the double \a {d} is equivalent to infinity.
    \relates <QtGlobal>
*/
Q_CORE_EXPORT bool qIsInf(double d) { return qt_is_inf(d); }

/*!
    Returns \c true if the double \a {d} is not a number (NaN).
    \relates <QtGlobal>
*/
Q_CORE_EXPORT bool qIsNaN(double d) { return qt_is_nan(d); }/*!
    Returns \c true if the float \a {f} is equivalent to infinity.
    \relates <QtGlobal>
*/
Q_CORE_EXPORT bool qIsInf(float f) { return qt_is_inf(f); }

/*!
    Returns \c true if the float \a {f} is not a number (NaN).
    \relates <QtGlobal>
*/
Q_CORE_EXPORT bool qIsNaN(float f) { return qt_is_nan(f); }

 

qnumeric_p.h 头文件中部分代码段

static inline bool qt_is_inf(float d)
{
    uchar *ch = (uchar *)&d;
    if (QSysInfo::ByteOrder == QSysInfo::BigEndian) {
        return (ch[0] & 0x7f) == 0x7f && ch[1] == 0x80;
    } else {
        return (ch[3] & 0x7f) == 0x7f && ch[2] == 0x80;
    }
}

static inline bool qt_is_nan(float d)
{
    uchar *ch = (uchar *)&d;
    if (QSysInfo::ByteOrder == QSysInfo::BigEndian) {
        return (ch[0] & 0x7f) == 0x7f && ch[1] > 0x80;
    } else {
        return (ch[3] & 0x7f) == 0x7f && ch[2] > 0x80;
    }
}

 

这么样的话,就很容易拷贝过来在 qt4中使用了,

    // 数值范围判断 参考 qt5.6源代码 qnumeric.cpp  qnumeric_p.h
    // 源码目录: C:\Qt\Qt5.6.0\5.6\Src\qtbase\src\corelib\global
    static inline bool qIsInf_craig(float f) { return qt_is_inf(f); }

    static inline bool qt_is_inf(float d)
    {
        uchar *ch = (uchar *)&d;
        if (QSysInfo::ByteOrder == QSysInfo::BigEndian) {
            return (ch[0] & 0x7f) == 0x7f && ch[1] == 0x80;
        } else {
            return (ch[3] & 0x7f) == 0x7f && ch[2] == 0x80;
        }
    }

    static inline bool qIsNaN_craig(float f) { return qt_is_nan(f); }

    static inline bool qt_is_nan(float d)
    {
        uchar *ch = (uchar *)&d;
        if (QSysInfo::ByteOrder == QSysInfo::BigEndian) {
            return (ch[0] & 0x7f) == 0x7f && ch[1] > 0x80;
        } else {
            return (ch[3] & 0x7f) == 0x7f && ch[2] > 0x80;
        }
    }

 

QSysInfo::ByteOrder == QSysInfo::BigEndian

需要包含 头文件 #include <QSysInfo>

 

目前使用还可以,还没有出现不正确的结果,需要继续观察

感谢上帝,

posted @ 2019-12-24 11:11  仆人  阅读(1695)  评论(0编辑  收藏  举报