函式
<cmath> <ctgmath>

llround

long long int llround  (double x);long long int llroundf (float x);long long int llroundl (long double x);
long long int llround (double x);long long int llround (float x);long long int llround (long double x);long long int llround (T x);           // additional overloads for integral types
四捨五入到最近的整數並轉換為 long long int
返回與 x 值最接近的整數值,中間情況(halfway cases)則向遠離零的方向舍入。

舍入後的值以 long long int 型別返回。關於返回 long int 的等效函式,請參閱 lround

標頭檔案 <tgmath.h> 提供了此函式的型別通用宏版本。
此標頭檔案 (<cmath>) 為整型提供了其他過載:這些過載在計算前有效地將 x 轉換為 double(為任何整型 T 定義)。

引數

x
要取整的值。

返回值

x 的值四捨五入到最近的整數,並轉換為 long long int 型別的值。
如果取整後的值超出了返回型別的範圍,則返回的值是未指定的,並且可能會發生定義域錯誤或上溢範圍錯誤(或者,根據具體實現,也可能什麼都不發生)。

如果發生定義域錯誤
- 並且 math_errhandling 設定了 MATH_ERRNO:全域性變數 errno 會被設定為 EDOM
- 並且 math_errhandling 設定了 MATH_ERREXCEPT:將引發 FE_INVALID

如果發生上溢範圍錯誤
- 並且 math_errhandling 設定了 MATH_ERRNO:全域性變數 errno 被設定為 ERANGE
- 並且 math_errhandling 設定了 MATH_ERREXCEPT:將引發 FE_OVERFLOW

示例

1
2
3
4
5
6
7
8
9
10
11
12
/* llround example */
#include <stdio.h>      /* printf */
#include <math.h>       /* lround */

int main ()
{
  printf ( "llround (2.3) = %lld\n", llround(2.3) );
  printf ( "llround (3.8) = %lld\n", llround(3.8) );
  printf ( "llround (-2.3) = %lld\n", llround(-2.3) );
  printf ( "llround (-3.8) = %lld\n", llround(-3.8) );
  return 0;
}

可能的輸出

Rounding using to-nearest rounding:
llround (2.3) = 2
llround (3.8) = 4
llround (-2.3) = -2
llround (-3.8) = -4


另見