इस खंड में हम देखेंगे कि fmax () और fmin () को C++ में कैसे बदलें। fmax() और fmin() cmath हेडर फाइल में मौजूद हैं।
यह फ़ंक्शन फ्लोट, या डबल या लॉन्ग डबल के दो मान लेता है और क्रमशः fmax () और fmin () का उपयोग करके अधिकतम या न्यूनतम देता है।
यदि तर्क प्रकार भिन्न हैं, जैसे यदि कोई फ्लोट और डबल की तुलना करना चाहता है, या फ्लोट के साथ लंबे डबल की तुलना करना चाहता है, तो फ़ंक्शन उस मान में निहित रूप से टाइपकास्ट करता है, फिर संबंधित मान देता है।
उदाहरण
#include <cmath>
#include <iomanip>
#include <iostream>
using namespace std;
main() {
double res;
//uses of fmax()
res = fmax(50.0, 10.0); //compare for both positive value
cout << fixed << setprecision(4) << "fmax(50.0, 10.0) = " << res << endl;
res = fmax(-50.0, 10.0); //comparison between opposite sign
cout << fixed << setprecision(4) << "fmax(-50.0, 10.0) = " << res << endl;
res = fmax(-50.0, -10.0); //compare when both are negative
cout << fixed << setprecision(4) << "fmax(-50.0, -10.0) = " << res << endl;
//uses of fmin()
res = fmin(50.0, 10.0); //compare for both positive value
cout << fixed << setprecision(4) << "fmin(50.0, 10.0) = " << res << endl;
res = fmin(-50.0, 10.0); //comparison between opposite sign
cout << fixed << setprecision(4) << "fmin(-50.0, 10.0) = " << res << endl;
res = fmin(-50.0, -10.0); //compare when both are negative
cout << fixed << setprecision(4) << "fmin(-50.0, -10.0) = " << res << endl;
} आउटपुट
fmax(50.0, 10.0) = 50.0000 fmax(-50.0, 10.0) = 10.0000 fmax(-50.0, -10.0) = -10.0000 fmin(50.0, 10.0) = 10.0000 fmin(-50.0, 10.0) = -50.0000 fmin(-50.0, -10.0) = -50.0000