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


C++ fmin()用法及代碼示例


C++ fmin() 函數

fmin() 函數是 cmath 頭文件的庫函數,用於求給定數字的最小值,它接受兩個數字並返回較小的一個。

fmin() 函數的語法:

    fmin(x, y);

參數: x, y– 是用於查找較小值的數字。

返回值: double– 它返回 double 值,即較小的值xy

例:

    Input:
    float x = 20;
    float y = 10;

    Function call:
    fmin(x,y);

    Output:
    10

C++代碼演示fmin()函數的例子

// C++ code to demonstrate the example of 
// fmin() function
#include <iostream>
#include <cmath>
using namespace std;

// main() section
int main()
{
    float x;
    float y;
    float result;
    
    //input the numbers
    cout<<"Enter first number:";
    cin>>x;
    cout<<"Enter second number:";
    cin>>y;
    
    //finding the minimum number
    result = fmin(x,y);
    cout<<"fmin("<<x<<","<<y<<"):"<<result;
    cout<<endl;
    
    return 0;
}

輸出

First run:
Enter first number:10
Enter second number:-20 
fmin(10,-20):-20 

Second run:
Enter first number:10
Enter second number:20
fmin(10,20):10

Third run:
Enter first number:123.90
Enter second number:121.88
fmin(123.9,121.88):121.88 

Fourth run:
Enter first number:-123.45 
Enter second number:-121.56 
fmin(-123.45,-121.56):-123.45 


相關用法


注:本文由純淨天空篩選整理自 fmin() function with example in C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。