เราจะมาดูกันว่าฟังก์ชั่น fdim() ใน C++ คืออะไร ฟังก์ชัน fdim() ใช้เพื่อคืนค่าความแตกต่างเชิงบวกระหว่างสองอาร์กิวเมนต์ที่กำหนด ถ้าอาร์กิวเมนต์สองตัวคือ a และ b ตามลำดับ และถ้า a>b อาร์กิวเมนต์จะคืนค่า a – b มิฉะนั้นจะคืนค่า 0
ตัวอย่าง
#include <cmath>
#include <iostream>
using namespace std;
main() {
cout << "fdim of (5.0, 2.0) is " << fdim(5.0, 2.0) << endl; //positive difference
cout << "fdim of (2.0, 5.0) is " << fdim(2.0, 5.0) << endl; //this will return 0
cout << "fdim of (-5.0, -2.0) is " << fdim(-5.0, -2.0) << endl; //this will return 0
cout << "fdim of (-2.0, -5.0) is " << fdim(-2.0, -5.0) << endl; //positive difference
} ผลลัพธ์
fdim of (5.0, 2.0) is 3 fdim of (2.0, 5.0) is 0 fdim of (-5.0, -2.0) is 0 fdim of (-2.0, -5.0) is 3