在C++中,ISLISSCORLL()是一个预定义函数。 数学H .用于检查第一个浮点数是否小于或等于第二个浮点数。与简单比较相比,它提供的优势是,它在进行比较时不会引发浮点异常。例如,如果两个参数中的一个是NaN,那么它将返回false而不会引发异常。
null
语法:
bool isgreaterequal(a, b)
参数:
- a、 b=> 这两个参数的值是我们想要比较的。
结果:
- 如果a<=b,函数将返回true,否则返回false。
错误:
此函数不会发生错误。 例外情况:
- 如果 A. 或 B 或者两者都是NaN,则函数引发异常并返回false(0)。
下面给出了这方面的说明
// CPP code to illustrate // the exception of function #include <bits/stdc++.h> using namespace std; int main() { // Take any values float a = 5.5; double f1 = nan( "1" ); bool result; // Since f1 value is NaN so // with any value of a, the function // always return false(0) result = islessequal(a, f1); cout << a << " islessequal " << f1 << ": " << result; return 0; } |
输出:
5.5 islessequal nan: 0
例如:
- 项目1:
// CPP code to illustrate
// the use of islessequal function
#include <bits/stdc++.h>
using
namespace
std;
int
main()
{
// Take two any values
float
a, b;
bool
result;
a = 5.2;
b = 8.5;
// Since 'a' is less than
// equal to 'b' so answer
// is true(1)
result = islessequal(a, b);
cout << a <<
" islessequal to "
<< b
<<
": "
<< result << endl;
int
x = 8;
int
y = 5;
// Since 'x' is not less
// than equal to 'y' so answer
// is false(0)
result = islessequal(x, y);
cout << x <<
" islessequal to "
<< y
<<
": "
<< result;
return
0;
}
注: 使用此函数,您还可以将任何数据类型与任何其他数据类型进行比较。
- 项目2:
// CPP code to illustrate
// the use of islessequal function
#include <bits/stdc++.h>
using
namespace
std;
int
main()
{
// Take any two values
bool
result;
float
a = 80.23;
int
b = 82;
// Since 'a' is less
// than equal to 'b' so answer
// is true(1)
result = islessequal(a, b);
cout << a <<
" islessequal to "
<< b
<<
": "
<< result << endl;
char
x =
'c'
;
// Since 'c' ascii value(99) is not
// less than variable a so answer
// is false(0)
result = islessequal(x, a);
cout << x <<
" islessequal to "
<< a
<<
": "
<< result;
return
0;
}
输出:
80.23 islessequal to 82: 1 c islessequal to 80.23: 0
应用: 在很多应用程序中,我们可以使用islessequal()函数来比较两个类似于中使用的值 虽然 循环打印前10个自然数。
// CPP code to illustrate // the use of islessequal function #include <bits/stdc++.h> using namespace std; int main() { int i = 1; while (islessequal(i, 10)) { cout << i << " " ; i++; } return 0; } |
输出:
1 2 3 4 5 6 7 8 9 10
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END