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

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

ฟังก์ชันไลบรารี C/C++ double fabs(double x) คืนค่าค่าสัมบูรณ์ (absolute value) ของ x พารามิเตอร์ x คือค่าจุดทศนิยม ฟังก์ชันนี้คืนค่าค่าสัมบูรณ์ของ x การประกาศฟังก์ชัน fabs() ดังนี้:

double fabs(double x)

ตัวอย่างต่อไปนี้แสดงการใช้งาน fabs()

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

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

int main() {
    int a, b;
    a = 1234;
    b = -344;
    cout << "The absolute value of " << a << " is " << fabs(a) << endl;
    cout << "The absolute value of " << b << " is " << fabs(b) << endl;
    return 0;
}

ผลลัพธ์

The absolute value of 1234 is 1234
The absolute value of -344 is 344