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


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++。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。