llrint()函数以及C ++中的示例

C ++ llrint()函数 (C++ llrint() function)

llrint() function is a library function of cmath header, it is used to round the given value and convert it to a long long integer. It accepts a value (float, double, or long double) and returns a long long integer value after rounding it based on the rounding direction specified by fegetround() function.

llrint()函数cmath标头的库函数,用于舍入给定值并将其转换为长整型整数。 它接受一个值( float , double或long double ),并在根据fegetround()函数指定的舍入方向将其舍入后返回一个长整型整数值。

Syntax of llrint() function:

llrint()函数的语法:

C++11:

C ++ 11:

    long long int llrint (double x);
    long long int llrint (float x);
    long long int llrint (long double x);
    long long int llrint (T x); 

Parameter(s):

参数:

  • x – represents the value to round.

    x –表示要取整的值。

Return value:

返回值:

The returns type of this function is long long int, it returns a long long integer value rounded to nearby integral.

此函数的返回类型为long long int ,它返回一个四舍五入到附近整数的long long整数值。

Example:

例:

    Input:
    float x = 123.4f;
    
    Function call:
    llrint(x);
    
    Output:
    123

    Input:
    float x = 123.5f;
    
    Function call:
    llrint(x);
    
    Output:
    124

C ++代码演示llrint()函数的示例 (C++ code to demonstrate the example of llrint() function)

// C++ code to demonstrate the example of
// llrint() function

#include <iostream>
#include <cmath>
#include <fenv.h> // for fegetround()
using namespace std;

int main()
{
    float x = 0.0f;

    cout << "Specified rounding is: ";
    switch (fegetround()) {
    case FE_DOWNWARD:
        cout << "Downward" << endl;
        break;
    case FE_TONEAREST:
        cout << "To-nearest" << endl;
        break;
    case FE_TOWARDZERO:
        cout << "Toward-zero" << endl;
        break;
    case FE_UPWARD:
        cout << "Upward" << endl;
        break;
    default:
        cout << "Unknown" << endl;
    }

    x = 123.4f;
    cout << "llrint(" << x << "): " << llrint(x) << endl;

    x = 123.5f;
    cout << "llrint(" << x << "): " << llrint(x) << endl;

    x = 123.6f;
    cout << "llrint(" << x << "): " << llrint(x) << endl;

    x = -123.4f;
    cout << "llrint(" << x << "): " << llrint(x) << endl;

    x = -123.5f;
    cout << "llrint(" << x << "): " << llrint(x) << endl;

    x = -123.6f;
    cout << "llrint(" << x << "): " << llrint(x) << endl;

    return 0;
}

Output

输出量

Specified rounding is: To-nearest
llrint(123.4): 123
llrint(123.5): 124
llrint(123.6): 124
llrint(-123.4): -123
llrint(-123.5): -124
llrint(-123.6): -124

Reference: C++ llrint() function

参考: C ++ llrint()函数

翻译自: https://www.includehelp.com/cpp-tutorial/llrint-function-with-example.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值