当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C++ fmax() and fmin()用法及代码示例


在cmath头文件中定义了fmax()和fmin()函数。

  1. fmax()函数:该函数的语法为:
    double fmax (double x, double y);
    float fmax (float x, float y);
    long double fmax (long double x, long double y);
    

    该函数的输入是float,double或long double类型的两个值。该函数返回两个输入值中的最大值。

    下面是示例C++程序,显示fmax()函数的工作:


    // CPP program to show working 
    // of fmax() function. 
      
    #include <cmath> 
    #include <iomanip> 
    #include <iostream> 
      
    using namespace std; 
      
    int main() 
    { 
        double val; 
      
        // Find maximum value when both the inputs 
        // are positive. 
        val = fmax(10.0, 1.0); 
        cout << fixed << setprecision(4) 
             << "fmax(10.0, 1.0) = " << val << "\n"; 
      
        // Find maximum value when inputs have 
        // opposite sign. 
        val = fmax(-10.0, 1.0); 
        cout << fixed << setprecision(4) 
             << "fmax(-10.0, 1.0) = " << val << "\n"; 
      
        // Find maximum value when both the inputs 
        // are negative. 
        val = fmax(-10.0, -1.0); 
        cout << fixed << setprecision(4) 
             << "fmax(-10.0, -1.0) = " << val << "\n"; 
      
        return 0; 
    }
    输出:
    fmax(10.0, 1.0) = 10.0000
    fmax(-10.0, 1.0) = 1.0000
    fmax(-10.0, -1.0) = -1.0000
    
  2. fmin()函数:该函数的语法为:
    double fmin (double x, double y);
    float fmin (float x, float y);
    long double fmin (long double x, long double y);
    

    该函数的输入是float,double或long double类型的两个值。该函数返回两个输入值中的最小值。

    下面是示例C++程序,显示fmin()函数的工作:

    // CPP program to show working 
    // of fmin() function. 
      
    #include <cmath> 
    #include <iomanip> 
    #include <iostream> 
      
    using namespace std; 
      
    int main() 
    { 
        double val; 
      
        // Find minimum value when both the inputs 
        // are positive. 
        val = fmin(10.0, 1.0); 
        cout << fixed << setprecision(4) 
             << "fmin(10.0, 1.0) = " << val << "\n"; 
      
        // Find minimum value when inputs have 
        // opposite sign. 
        val = fmin(-10.0, 1.0); 
        cout << fixed << setprecision(4) 
             << "fmin(-10.0, 1.0) = " << val << "\n"; 
      
        // Find minimum value when both the inputs 
        // are negative. 
        val = fmin(-10.0, -1.0); 
        cout << fixed << setprecision(4) 
             << "fmin(-10.0, -1.0) = " << val << "\n"; 
      
        return 0; 
    }
    输出:
    fmin(10.0, 1.0) = 1.0000
    fmin(-10.0, 1.0) = -10.0000
    fmin(-10.0, -1.0) = -10.0000
    
  3. 注意:考虑一下函数的参数为​​不同类型的情况。在这种情况下,参数首先由函数隐式类型转换,然后返回所需的最大值/最小值。
    下面是说明这种情况的示例C++程序:

    // CPP program to show working 
    // of fmin() and fmax()function 
    // when input values are of 
    // different data types. 
      
    #include <cmath> 
    #include <iomanip> 
    #include <iostream> 
      
    using namespace std; 
      
    int main() 
    { 
        double val; 
      
        // Find minimum value when one of the inputs 
        // is of type float and other is of type 
        // double. 
        val = fmin(10.0f, 1.0); 
        cout << fixed << setprecision(4) 
             << "fmin(10.0f, 1.0) = " << val << "\n"; 
      
        // Find maximum value when one of the inputs 
        // is of type float and other is of type 
        // double. 
        val = fmax(10.0, -1.0f); 
        cout << fixed << setprecision(4) 
             << "fmax(10.0, -1.0f) = " << val << "\n"; 
      
        return 0; 
    }
    输出:
    fmin(10.0f, 1.0) = 1.0000
    fmax(10.0, -1.0f) = 10.0000
    


相关用法


注:本文由纯净天空筛选整理自nik1996大神的英文原创作品 fmax() and fmin() in C++。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。