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 小时前
【单片机原理】第1章 微机基础知识,运算器,控制器,寄存器,微机工作过程,数制转换
算法
檀越剑指大厂1 小时前
【Python系列】Python中的`any`函数:检查“至少有一个”条件满足
开发语言·python
Crazy learner1 小时前
C 和 C++ 动态库的跨语言调用原理
c语言·c++
I_Am_Me_2 小时前
【JavaEE初阶】线程安全问题
开发语言·python
IT猿手2 小时前
基于PWLCM混沌映射的麋鹿群优化算法(Elk herd optimizer,EHO)的多无人机协同路径规划,MATLAB代码
算法·elk·机器学习·matlab·无人机·聚类·强化学习
运维&陈同学2 小时前
【Elasticsearch05】企业级日志分析系统ELK之集群工作原理
运维·开发语言·后端·python·elasticsearch·自动化·jenkins·哈希算法
金士顿4 小时前
MFC 文档模板 每个文档模板需要实例化吧
c++·mfc
ZVAyIVqt0UFji5 小时前
go-zero负载均衡实现原理
运维·开发语言·后端·golang·负载均衡
loop lee5 小时前
Nginx - 负载均衡及其配置(Balance)
java·开发语言·github
SomeB1oody5 小时前
【Rust自学】4.1. 所有权:栈内存 vs. 堆内存
开发语言·后端·rust