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

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

นี่คือฟังก์ชัน fdim() ใน C++ ใช้คืนค่าความต่างบวกระหว่างสองอาร์กิวเมนต์ ถ้าอาร์กิวเมนต์สองคือ 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; // ความต่างบวก
  cout << "fdim of (2.0, 5.0) is " << fdim(2.0, 5.0) << endl; // คืนค่า 0
  cout << "fdim of (-5.0, -2.0) is " << fdim(-5.0, -2.0) << endl; // คืนค่า 0
  cout << "fdim of (-2.0, -5.0) is " << fdim(-2.0, -5.0) << endl; // ความต่างบวก
}

ผลลัพธ์

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