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
相关推荐
nevergiveup_202413 分钟前
ORB-SLAM2 ---- Tracking::Relocalization()
笔记·算法·计算机视觉
我爱工作&工作love我13 分钟前
1436:数列分段II -整型二分
c++·算法
IU宝14 分钟前
类和对象——拷贝构造函数,赋值运算符重载(C++)
开发语言·c++
Root_Smile19 分钟前
【C++】构造函数
开发语言·c++
丫头,冲鸭!!!25 分钟前
内部排序和外部排序以及常见算法和时间复杂度
数据结构·算法·排序算法
Dola_Pan25 分钟前
十大经典排序算法-希尔排序与归并排序
数据结构·算法·排序算法
chenxiemin28 分钟前
卡尔曼滤波:从理论到应用的简介
python·算法·filter·卡尔曼滤波·kalman
源代码杀手34 分钟前
【Rust 编程语言工具】rustup-init.exe 安装与使用指南
开发语言·后端·rust
double丶flower1 小时前
HashMap与HashTable的区别以及HashMap的底层原理
java·开发语言
竹下为生1 小时前
LeetCode --- 143周赛
算法·leetcode·职场和发展