Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

ฟังก์ชัน isnormal() ใน C++

ในส่วนนี้เราจะดูฟังก์ชัน isnormal() ใน C++ ฟังก์ชันนี้อยู่ในไลบรารี cmath ใช้ตรวจสอบว่าเลขเป็น "ปกติ" (normal) หรือไม่ เลขที่ไม่ปกติคือ 0, infinity, หรือ NaN

ฟังก์ชันรับ float, double, หรือ long double คืนค่า 1 ถ้าเป็นเลขปกติ มิฉะนั้นคืน 0

ตัวอย่างโค้ด

#include <iostream>
#include <cmath>
using namespace std;

int main() {
    cout << "isnormal(" << 5.23 << "): " << isnormal(5.23) << endl;
    cout << "isnormal(" << 0.00 << "): " << isnormal(0.00) << endl;
    cout << "isnormal(" << 2.0/0.0 << "): " << isnormal(2.0/0.0) << endl;
}

ผลลัพธ์

isnormal(5.23): 1
isnormal(0): 0
isnormal(inf): 0