📜  C++中的fmax()和fmin()

📅  最后修改于: 2021-05-30 05:16:45             🧑  作者: Mango

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

  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 
    #include 
    #include 
      
    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 
    #include 
    #include 
      
    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
    

    注意:考虑函数的参数为不同类型的情况。在这种情况下,参数首先由函数隐式类型转换,然后返回所需的最大值/最小值。
    下面是说明这种情况的示例C++程序:

    // CPP program to show working
    // of fmin() and fmax()function
    // when input values are of
    // different data types.
      
    #include 
    #include 
    #include 
      
    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
    
    要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”