C++ 中的 isinf() 函数


在本文中我们将讨论 C++ 中的 isinf() 函数,它的语法、工作原理和返回值。

isinf() 是 C++ 中的内置函数,属于头文件,该函数用于检查传入它的变量是否是无穷大,无论该数字是负无穷大还是正无穷大。如果该数字是无穷大,则该函数返回非零值 (true),如果不是,则它传递零 (false)。此外,如果该数字为 NAN,那么该函数也将返回 0。

语法

bool isinf(float n);

bool isinf(double n);

bool isinf(long double n);

此函数仅接受一个浮点数。

返回值

函数返回布尔值,对于 false(不是无穷大)返回 0,对于 true(无穷大)返回 1。

示例

 实时演示

#include <iostream>
#include <cmath>
using namespace std;
int main() {
   float a = 0.0, b = 10.0;
   isinf(a/b)?cout<<"\nInfinte":cout<<"\nFinite"; //check the number is infinte or finite
   isinf(b/a)?cout<<"\nInfinite":cout<<"\nFinite";
}

输出

如果我们运行以上代码,它将生成以下输出 −

Finite
Infinite

示例

 实时演示

#include <iostream>
#include <cmath>
using namespace std; int main() {
   float a = 0.0;
   cout<<isinf(a);
   cout<<isinf(sqrt(-1.0));
}

输出

如果我们运行以上代码,它将生成以下输出 −

0 0

更新时间: 2020-02-28

2K 多次阅读

开启您的 职业生涯

通过完成课程获得认证

开始
广告
© . All rights reserved.