C++ llrint() 函數
llrint() 函數是 cmath 頭文件的庫函數,用於對給定值進行四舍五入並將其轉換為 long long 整數。它接受一個值 (float
,double
, 或者long double
) 並根據指定的舍入方向舍入後返回一個長整型值fegetround()
函數。
llrint() 函數的語法:
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);
參數:
x
– 表示要舍入的值。
返回值:
這個函數的返回類型是long long int
,它返回一個長整數值,四舍五入到附近的整數。
例:
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
#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;
}
輸出
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
參考:C++ llrint() 函數
相關用法
- C++ llround()用法及代碼示例
- C++ lldiv()用法及代碼示例
- C++ llabs()用法及代碼示例
- C++ list assign()用法及代碼示例
- C++ log2()用法及代碼示例
- C++ lrint() and llrint()用法及代碼示例
- C++ list::remove()、list::remove_if()用法及代碼示例
- C++ lrint()用法及代碼示例
- C++ list back()用法及代碼示例
- C++ list merge()用法及代碼示例
- C++ ldiv()用法及代碼示例
- C++ list remove()用法及代碼示例
- C++ log1p()用法及代碼示例
- C++ list push_back()用法及代碼示例
- C++ list::operator=用法及代碼示例
- C++ list erase()用法及代碼示例
- C++ list::empty()、list::size()用法及代碼示例
- C++ list resize()用法及代碼示例
- C++ list pop_front()用法及代碼示例
- C++ list empty()用法及代碼示例
注:本文由純淨天空篩選整理自 llrint() Function with Example in C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。