C++11标准模板(STL)- 常用数学函数 - 宏常量 - 求值得到float类型的安静NaN(NAN)
常用数学函数
求值得到float类型的安静NaN
NAN
定义于头文件 | ||
#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