C++11标准模板(STL)- 常用数学函数 - 宏常量 - 求值得到float类型的安静NaN(NAN)

常用数学函数

求值得到float类型的安静NaN

复制代码
NAN

|------------------------------------------|---|---------|
| 定义于头文件 <math.h> | | |
| #define NAN /*implementation defined*/ | | (C99 起) |

NAN 展开成求值为安静非数( QNaN )的 float 类型常量表达式。若实现不支持 QNaN ,则不定义此宏。

用于打印 NaN 的风格是实现定义的。

注意

有许多不同的 NaN 值,区别于其载荷与其符号位。宏 NAN 所生成的 NaN 的载荷与符号位的内容是实现定义的。

示例

显示用于打印 NaN 的风格和 IEEE 格式。

调用示例

复制代码
#include <iostream>
#include <cstdlib>
#include <typeinfo>
#include <cinttypes>
#include <cmath>
#include <math.h>
#include <tgmath.h>

int main()
{
    std::cout << std::boolalpha;

    //对给定的浮点值分类
    std::cout << "std::fpclassify(" << NAN << "):     "
              << std::fpclassify(NAN) << std::endl;
    std::cout << std::endl;

    //检查给定数是否具有有限值
    std::cout << "std::isfinite(" << NAN << "):     "
              << std::isfinite(NAN) << std::endl;
    std::cout << std::endl;

    //检查给定数是否是无穷大
    std::cout << "std::isinf(" << NAN << "):     "
              << std::isinf(NAN) << std::endl;
    std::cout << std::endl;

    //检查给定数是否是NaN
    std::cout << "std::isnan(" << NAN << "):     "
              << std::isnan(NAN) << std::endl;
    std::cout << std::endl;

    //检查给定数是否正规
    std::cout << "std::isnormal(" << NAN << "):     "
              << std::isnormal(NAN) << std::endl;
    std::cout << std::endl;

    //检查给定数是不是负数
    std::cout << "std::signbit(" << NAN << "):     "
              << std::signbit(NAN) << std::endl;
    std::cout << std::endl;

    const float fNumber = std::tan(10);
    const float fNumber1 = std::cos(fNumber);
    //检查第一个浮点参数是否大于第二个
    std::cout << "std::isgreater(" << NAN << " ," << fNumber1 << "):     "
              << std::isgreater(NAN, fNumber1) << std::endl;
    std::cout << std::endl;

    //检查第一个浮点参数是否大于等于第二个
    std::cout << "std::isgreaterequal(" << NAN << " ," << fNumber1 << "):     "
              << std::isgreaterequal(NAN, fNumber1) << std::endl;
    std::cout << std::endl;

    //检查第一个浮点参数是否小于第二个
    std::cout << "std::isless(" << NAN << " ," << fNumber1 << "):     "
              << std::isless(NAN, fNumber1) << std::endl;
    std::cout << std::endl;

    //检查第一个浮点参数是否小于或等于第二个
    std::cout << "std::islessequal(" << NAN << " ," << fNumber1 << "):     "
              << std::islessequal(NAN, fNumber1) << std::endl;
    std::cout << std::endl;

    //检查第一个浮点参数是否小于或大于第二个
    std::cout << "std::islessgreater(" << NAN << " ," << fNumber1 << "):     "
              << std::islessgreater(NAN, fNumber1) << std::endl;
    std::cout << std::endl;

    //检查两个浮点数值是否无序
    std::cout << "std::isunordered(" << NAN << " ," << fNumber1 << "):     "
              << std::isunordered(NAN, fNumber1) << std::endl;
    std::cout << std::endl;

    //确定浮点数 x 与 y 是否无序,即一或两个是 NaN ,从而无法有意义地彼此比较。
    for (int i = 0; i <= 10; i += 1)
    {
        const float fNumber = std::acos(-1) / 2 / i;
        const float fNumber1 = std::acos(fNumber) - i / 10;
        std::cout << "std::isunordered(" << fNumber1
                  << "," << NAN << "):   "
                  << std::isunordered(fNumber1, NAN) << std::endl;
    }
    std::cout << std::endl;

    for (int i = 0; i <= 10; i += 1)
    {
        const float fNumber = std::cos(-1) / 2 / i;
        const float fNumber1 = std::cos(fNumber) - i / 10;
        std::cout << "std::isunordered(" << fNumber1
                  << "," << NAN << "):   "
                  << std::isunordered(fNumber1, NAN) << std::endl;
    }
    std::cout << std::endl;

    for (int i = 0; i <= 10; i += 1)
    {
        const float fNumber = std::tan(i);
        const float fNumber1 = std::cos(fNumber) - i / 10;
        std::cout << "std::isunordered(" << fNumber1
                  << "," << NAN << "):   "
                  << std::isunordered(fNumber1, NAN) << std::endl;
    }
    std::cout << std::endl;

    for (int i = 0; i <= 10; i += 1)
    {
        const float fNumber = std::atan(i * std::acos(-1));
        const float fNumber1 = std::sin(fNumber) / std::cos(fNumber);
        std::cout << "std::isunordered(" << fNumber1
                  << "," << NAN << "):   "
                  << std::isunordered(fNumber1, NAN) << std::endl;
    }

    return 0;
}

输出

复制代码
std::fpclassify(nan):     256

std::isfinite(nan):     false

std::isinf(nan):     false

std::isnan(nan):     true

std::isnormal(nan):     false

std::signbit(nan):     false

std::isgreater(nan ,0.797075):     false

std::isgreaterequal(nan ,0.797075):     false

std::isless(nan ,0.797075):     false

std::islessequal(nan ,0.797075):     false

std::islessgreater(nan ,0.797075):     false

std::isunordered(nan ,0.797075):     true

std::isunordered(nan,nan):   true
std::isunordered(nan,nan):   true
std::isunordered(0.667457,nan):   true
std::isunordered(1.01973,nan):   true
std::isunordered(1.16723,nan):   true
std::isunordered(1.25123,nan):   true
std::isunordered(1.30591,nan):   true
std::isunordered(1.34447,nan):   true
std::isunordered(1.37316,nan):   true
std::isunordered(1.39536,nan):   true
std::isunordered(0.413063,nan):   true

std::isunordered(nan,nan):   true
std::isunordered(0.963731,nan):   true
std::isunordered(0.990891,nan):   true
std::isunordered(0.995948,nan):   true
std::isunordered(0.99772,nan):   true
std::isunordered(0.998541,nan):   true
std::isunordered(0.998987,nan):   true
std::isunordered(0.999255,nan):   true
std::isunordered(0.99943,nan):   true
std::isunordered(0.99955,nan):   true
std::isunordered(-0.0003649,nan):   true

std::isunordered(1,nan):   true
std::isunordered(0.0133882,nan):   true
std::isunordered(-0.57634,nan):   true
std::isunordered(0.989857,nan):   true
std::isunordered(0.401336,nan):   true
std::isunordered(-0.971594,nan):   true
std::isunordered(0.957956,nan):   true
std::isunordered(0.643719,nan):   true
std::isunordered(0.86954,nan):   true
std::isunordered(0.899437,nan):   true
std::isunordered(-0.202925,nan):   true

std::isunordered(0,nan):   true
std::isunordered(3.14159,nan):   true
std::isunordered(6.28319,nan):   true
std::isunordered(9.42477,nan):   true
std::isunordered(12.5664,nan):   true
std::isunordered(15.708,nan):   true
std::isunordered(18.8496,nan):   true
std::isunordered(21.9911,nan):   true
std::isunordered(25.1327,nan):   true
std::isunordered(28.2743,nan):   true
std::isunordered(31.4159,nan):   true
相关推荐
凯强同学1 分钟前
第十四届蓝桥杯大赛软件赛省赛Python 大学 C 组:6.棋盘
python·算法·蓝桥杯
我的大老婆2 分钟前
【Python】Python 环境 + Pycharm 编译器 官网免费下载安装(图文教程,新手安装,Windows 10 系统)
开发语言·windows·经验分享·python·青少年编程·pycharm
m0_555762907 分钟前
多人协同进行qt应用程序开发应该注意什么?
开发语言·qt
别来无恙20225 分钟前
算法设计学习4
c++·学习
wuqingshun31415928 分钟前
蓝桥杯 切割
数据结构·c++·算法·职场和发展·蓝桥杯
艾妮艾妮32 分钟前
C语言常见3种排序
java·c语言·开发语言·c++·算法·c#·排序算法
百度Geek说32 分钟前
前沿多模态模型开发与应用实战3:DeepSeek-VL2多模态理解大模型算法解析与功能抢先体验
算法
小王努力学编程34 分钟前
动态规划学习——回文子串系列问题【C++】
c++·学习·算法·leetcode·动态规划
A_ugust__38 分钟前
vue3项目使用 python +flask 打包成桌面应用
开发语言·python·flask
葵野寺42 分钟前
【多线程】synchronized锁升级和优化
java·开发语言·java-ee