当前位置: 首页 > article >正文

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


http://www.kler.cn/a/396948.html

相关文章:

  • linux企业中常用NFS、ftp服务
  • 24 年第十届数维杯国际数模竞赛赛题浅析
  • C/C++语言基础--initializer_list表达式、tuple元组、pair对组简介
  • IROS讲座:如何写出受欢迎的论文
  • FingerprintSimilarity和BulkTanimotoSimilarity的区别
  • Photoshop(PS)——人像磨皮
  • 硬件---4电感---基本概念与特性
  • Aria2-CVE-2023-39141漏洞分析
  • 使用 Caffeine 缓存并在业务方法上通过注解实现每3到5秒更新缓存
  • Python学习笔记(2)正则表达式
  • 《Java核心技术 卷I》用户界面AWT事件继承层次
  • 【网络】什么是交换机?switch
  • IDC机房服务器托管的费用组成
  • 【MyBatis操作数据库】XML配置
  • 自然语言处理(NLP)的开源生态系统:Hugging Face 原理和使用
  • [241115] Debian 12.8 发布 | Mistral AI 推出批量 API,成本降低 50%
  • “嵌入”在大语言模型中是解决把句子转换成向量表示的技术
  • Servlet⽣生命周期超级细(笔记)
  • 区间和 离散化 模板题
  • CentOS 升级 gcc 版本
  • 项目风险管理的3大要素
  • 如何基于Tesseract实现图片的文本识别
  • Redis 持久化机制 RDB 和 AOF 区别
  • 图像重建之深度学习重建
  • 安全见闻5
  • 深入理解UML通信图:概念、构成与使用示例