當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。