이 섹션에서는 C++에서 fmax() 및 fmin()을 변환하는 방법을 볼 것입니다. fmax() 및 fmin()은 cmath 헤더 파일에 있습니다.
이 함수는 float, double 또는 long double 유형의 두 값을 취하고 각각 fmax() 및 fmin()을 사용하여 최대값 또는 최소값을 반환합니다.
누군가가 float와 double을 비교하거나 long double을 float와 비교하려는 경우와 같이 인수 유형이 다른 경우 함수는 암시적으로 해당 값으로 유형 변환한 다음 해당 값을 반환합니다.
예시
#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