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


C++ lrint() and llrint()用法及代碼示例


lrint()函數使用當前的舍入模式將自變量中給出的分數值舍入為整數值。
在此,當前模式由函數fesetround()確定。

注意:此函數返回long int中的最終值。
用法:

lrint(double a);
lrint(float a);

參數:


  • lrint()函數采用雙精度或浮點型或整數值作為參數。

返回:

  • lrint()函數使用當前舍入模式將參數中給出的分數值舍入為整數值,並以long int形式返回值。在此,當前模式由函數fesetround()確定。默認情況下,舍入方向設置為“ to-nearest”。
    可以使用fesetround()進行更改。

錯誤:

  • 必須提供一個參數,否則將給調用“ lrint()”這樣的錯誤提供不匹配的函數。

#代碼1

// CPP code to illustrate 
// the functionality of lrint() 
#include <cfenv> 
#include <cmath> 
#include <iostream> 
using namespace std; 
  
int main() 
{ 
    int a = 15; 
    long int answer; 
  
    // setting rounding direction to DOWNWARD 
    fesetround(FE_DOWNWARD); 
    answer = lrint(a); 
    cout << "Downward rounding of " << a  
         << " is " << answer << endl; 
  
    return 0; 
}

輸出:

Downward rounding of 15 is 15

#代碼2

// CPP code to illustrate 
// the functionality of lrint() 
#include <cfenv> 
#include <cmath> 
#include <iostream> 
using namespace std; 
  
int main() 
{ 
    double a; 
    long int answer; 
  
    // By default, the rounding direction 
    // is set to 'to-nearest'. 
    // fesetround(FE_TONEAREST) 
    a = 50.35; 
    answer = lrint(a); 
    cout << "Nearest rounding of " << a  
         << " is " << answer << endl; 
  
    // mid values are rounded off to higher integer 
    a = 50.5; 
    answer = lrint(a); 
    cout << "Nearest rounding of " << a  
         << " is " << answer << endl; 
  
    return 0; 
}

輸出:

Nearest rounding of 50.35 is 50
Nearest rounding of 50.5 is 50

#代碼3

// CPP code to illustrate 
// the functionality of lrint() 
#include <cfenv> 
#include <cmath> 
#include <iostream> 
using namespace std; 
  
int main() 
{ 
    double a; 
    long int answer; 
  
    // Now, the rounding direction 
    // is set to UPWARD 
    fesetround(FE_UPWARD); 
    a = 50.3; 
    answer = lrint(a); 
    cout << "Upward rounding of " << a  
         << " is " << answer << endl; 
  
    // Now, the rounding direction 
    //  is set to DOWNWARD 
    fesetround(FE_DOWNWARD); 
    a = 50.88; 
    answer = lrint(a); 
    cout << "Downward rounding of " << a  
         << " is " << answer << endl; 
  
    return 0; 
}

輸出:

Upward rounding of 50.3 is 51
Downward rounding of 50.88 is 50

C++中的llrint()


llrint()函數使用當前的舍入模式將自變量中給出的分數值舍入為整數值。
在此,當前模式由函數fesetround()確定。

注意:因此函數以long long int返回值
用法:

llrint(double a);
llrint(float a);

參數:

  • llrint()函數采用雙精度或浮點型或整數值作為參數。

返回:

  • llrint()函數將自變量中給出的小數值舍入為
    使用當前舍入模式的整數值,並在
    long long int。
    在此,當前模式由函數fesetround()確定。
    默認情況下,舍入方向設置為“ to-nearest”。
    可以使用fesetround()進行更改。

錯誤:

  • 必須提供一個參數,否則將給調用“ llrint()”這樣的錯誤提供不匹配的函數。

#代碼1

// CPP code to illustrate 
// the functionality of llrint() 
#include <cfenv> 
#include <cmath> 
#include <iostream> 
using namespace std; 
  
int main() 
{ 
    int a = 15; 
    long long int answer; 
  
    // setting rounding direction to DOWNWARD 
    fesetround(FE_DOWNWARD); 
    answer = llrint(a); 
    cout << "Downward rounding of " << a  
         << " is " << answer << endl; 
  
    return 0; 
}

輸出:

Downward rounding of 15 is 15

#代碼2

// CPP code to illustrate 
// the functionality of llrint() 
#include <cfenv> 
#include <cmath> 
#include <iostream> 
using namespace std; 
  
int main() 
{ 
    double a; 
    long long int answer; 
  
    // By default, the rounding direction is  
    // set to 'to-nearest'. fesetround(FE_TONEAREST) 
    a = 50.35; 
    answer = llrint(a); 
    cout << "Nearest rounding of " << a  
         << " is " << answer << endl; 
  
    // mid values are rounded off to higher integer 
    a = 50.5; 
    answer = llrint(a); 
    cout << "Nearest rounding of " << a  
         << " is " << answer << endl; 
  
    return 0; 
}

輸出:

Nearest rounding of 50.35 is 50
Nearest rounding of 50.5 is 50

#代碼3

// CPP code to illustrate 
// the functionality of llrint() 
#include <cfenv> 
#include <cmath> 
#include <iostream> 
using namespace std; 
  
int main() 
{ 
    double a; 
    long long int answer; 
  
    // Now, the rounding direction 
    // is set to UPWARD 
    fesetround(FE_UPWARD); 
    a = 50.3; 
    answer = llrint(a); 
    cout << "Upward rounding of " << a  
         << " is " << answer << endl; 
  
    // Now, the rounding direction is set to DOWNWARD 
    fesetround(FE_DOWNWARD); 
    a = 50.88; 
    answer = llrint(a); 
    cout << "Downward rounding of " << a  
         << " is " << answer << endl; 
  
    return 0; 
}

輸出:

Upward rounding of 50.3 is 51
Downward rounding of 50.88 is 50


相關用法


注:本文由純淨天空篩選整理自pawan_asipu大神的英文原創作品 lrint() and llrint() in C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。