ในบทความนี้เราจะพูดถึงการทำงาน ไวยากรณ์และตัวอย่างของฟังก์ชัน isfinite() ใน C++
isfinite() เป็นฟังก์ชัน inbuilt ใน C ++ ซึ่งอยู่ภายใต้ไฟล์ส่วนหัว ฟังก์ชัน isfinite() ใช้สำหรับตรวจสอบและส่งคืนว่าจำนวนที่กำหนดมีจำกัดหรือไม่ จำนวนจำกัดคือจำนวนลอยตัวที่ไม่ใช่อนันต์หรือน่าน (ไม่ใช่ตัวเลข)
ไวยากรณ์
bool isfinite(float n);
หรือ
bool isfinite(double n);
หรือ
bool isfinite(long double n);
ฟังก์ชันนี้มีเพียง 1 พารามิเตอร์ n ซึ่งเป็นค่าที่เราต้องตรวจสอบว่ามีค่าจำกัดหรือไม่
คืนค่า
ฟังก์ชันส่งคืนค่าบูลีน 0(เท็จ) หากจำนวนไม่จำกัดและ 1 (จริง) หากจำนวนดังกล่าวมีจำกัด
ตัวอย่าง
#include <iostream>
#include <cmath>
using namespace std;
int main() {
float a = 10.0, b = 0.1, c = 0.0;
isfinite(a/b)?cout<<"\nThe result of a/b is finite":cout<<"\nThe result of a/b is not finite";
isfinite(a/c)?cout<<"\nThe result of a/c is finite":cout<<"\nThe result of a/c is not finite";
} ผลลัพธ์
หากเราเรียกใช้โค้ดข้างต้น มันจะสร้างผลลัพธ์ต่อไปนี้ -
The result of a/b is finite The result of a/c is not finite
ตัวอย่าง
#include <iostream>
#include <cmath>
using namespace std;
int main() {
float c = 0.0, d = -1.0;
//check the number is infinte or finite
isfinite(c)?cout<<"\nFinite number":cout<<"\nNot a finite number";
cout<<isfinite(sqrt(d)); //Result will be -NAN
} ผลลัพธ์
หากเราเรียกใช้โค้ดข้างต้น มันจะสร้างผลลัพธ์ต่อไปนี้ -
Finite number 0
หมายเหตุ - สแควร์รูทของ -1.0 จะคืนค่า nan