zoukankan      html  css  js  c++  java
  • 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:QtQt5.6.05.6Srcqtbasesrccorelibglobal

    三个源文件:

    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.
        
    elates <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).
        
    elates <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.
        
    elates <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).
        
    elates <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:QtQt5.6.05.6Srcqtbasesrccorelibglobal
        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>

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

    感谢上帝,

  • 相关阅读:
    白话LINQ系列2以代码演进方式学习LINQ必备条件
    《Entity Framework 6 Recipes》中文翻译系列 (9) 第二章 实体数据建模基础之继承关系映射TPH
    Linq To Sqlite 一一二二
    《Entity Framework 6 Recipes》中文翻译系列 (10) 第二章 实体数据建模基础之两实体间Isa和Hasa关系建模、嵌入值映射
    《Entity Framework 6 Recipes》中文翻译系列 (14) 第三章 查询之查询中设置默认值和存储过程返回多结果集
    《Entity Framework 6 Recipes》中文翻译系列 (13) 第三章 查询之使用Entity SQL
    白话LINQ系列1什么是LINQ?
    《Entity Framework 6 Recipes》翻译系列 (3) 第二章 实体数据建模基础之创建一个简单的模型
    JS 实现trim()
    mysql表切换引擎的几种方法
  • 原文地址:https://www.cnblogs.com/craigtao/p/12090420.html
Copyright © 2011-2022 走看看