rint函数 java_rint()函数与C ++中的示例

rint函数 java

C ++ rint()函数 (C++ rint() function)

rint() function is a library function of cmath header. It is used to round the given value to an integral value based on the specified direction by fegetround() function. It accepts a parameter and returns the rounded value.

rint()函数cmath标头的库函数。 它用于通过fegetround()函数根据指定的方向将给定值舍入为整数值。 它接受一个参数并返回四舍五入的值。

Syntax of rint() function:

rint()函数的语法:

C++11:

C ++ 11:

     double rint (double x);
      float rint (float x);
long double rint (long double x);
     double rint (T x); 

Parameter(s):

参数:

  • x – represents the value to round.

    x –表示要取整的值。

Return value:

返回值:

It returns rounded (to an integral value) value.

它返回四舍五入(至整数值)的值。

Example:

例:

    Input:
    double x = 123.4;
    
    Function call:
    rint(x);
    
    Output:
    123

    Input:
    double x = 123.5;
    
    Function call:
    rint(x);
    
    Output:
    124

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

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

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

int main()
{
    double x = 0.0;

    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.4;
    cout << "rint(" << x << "): " << rint(x) << endl;

    x = 123.5;
    cout << "rint(" << x << "): " << rint(x) << endl;

    x = 123.6;
    cout << "rint(" << x << "): " << rint(x) << endl;

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

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

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

    return 0;
}

Output

输出量

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

Reference: C++ rint() function

参考: C ++ rint()函数

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

rint函数 java

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值