C++ Reference: Standard C++ Library reference: C Library: cmath: llround

发布于:2022-12-04 ⋅ 阅读:(276) ⋅ 点赞:(0)

C++官网参考链接:https://cplusplus.com/reference/cmath/llround/

函数 
<cmath> <ctgmath>
llround
C99
long long int llround(double x);      
long long int llroundf(float x);
long long int llroundl(long double x);
C++11
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整型 
返回在值上最接近x的整数值,中间的情况舍入远离0。
舍入值作为long long int类型的值返回。参见lround获得返回long int类型的值的等效函数。
C99
头文件<tgmath.h>提供了该函数的类型泛型宏版本。 
C++11
在此头文件(<cmath>)中为整型(integral types)提供了额外的重载:这些重载在计算之前有效地将x转换为double类型(定义为T为任何整型(integral types))。

形参
x
将要舍入的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

用例
/* 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;
}
可能的输出:

另请参考
llrint      Round and cast to long long integer (function) (舍入并转换为long long整数(函数))
round    Round to nearest (function) (舍入到最近(函数))
lround   Round to nearest and cast to long integer (function) (舍入到最近并转换为long整数(函数))
nearbyint    Round to nearby integral value (function) (舍入到附近整数(函数))
floor            Round down value (function) (向下舍入取值(函数))
ceil              Round up value (function) (向上舍入取值(函数))
trunc           Truncate value (function) (截断取值(函数))

本文含有隐藏内容,请 开通VIP 后查看