函式
<cmath> <ctgmath>

lround

long int lround  (double x);long int lroundf (float x);long int lroundl (long double x);
long int lround (double x);long int lround (float x);long int lround (long double x);long int lround (T x);           // additional overloads for integral types
四捨五入到最近整數並轉換為長整數
返回最接近 x 的整數值,當值為一半時向遠離零的方向舍入。

返回的舍入值型別為 long int。有關返回 long long int 的等效函式,請參閱 llround

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

引數

x
要取整的值。

返回值

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

如果發生定義域錯誤
- 並且 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
/* lround example */
#include <stdio.h>      /* printf */
#include <math.h>       /* lround */

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

可能的輸出

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


另見