c++ 圆整(取整)相关函数大全(rint lrint llrint round ceil floor trunc)

1、圆整方向模式:

描述
FE_DOWNWARD Round downward.(向下取整)
FE_TONEAREST Round to nearest.(向最近取整)
FE_TOWARDZERO Round toward zero.(向0取整)
FE_UPWARD Round upward.(向上取整)

2、int fesetround (int rdir);(#include )
将当前浮点数的圆整方向模式设置为rdir

3、int fegetround (void); (#include )
获得当前取整方向。

4、rint:使用fegetround 得到的取整方向将x取整为整数(注意返回类型并不是整型)
函数声明:

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

代码示例:

/* rint example */
#include <stdio.h>      /* printf */
#include <fenv.h>       /* fegetround, FE_* */
#include <math.h>       /* rint */

int main ()
{
  printf ("rounding using ");
  switch (fegetround()) {
    case FE_DOWNWARD: printf ("downward"); break;
    case FE_TONEAREST: printf ("to-nearest"); break;
    case FE_TOWARDZERO: printf ("toward-zero"); break;
    case FE_UPWARD: printf ("upward"); break;
    default: printf ("unknown");
  }
  printf (" rounding:\n");

  printf ( "rint (2.3) = %.1f\n", rint(2.3) );
  printf ( "rint (3.8) = %.1f\n", rint(3.8) );
  printf ( "rint (-2.3) = %.1f\n", rint(-2.3) );
  printf ( "rint (-3.8) = %.1f\n", rint(-3.8) );
  return 0;
}

运行结果:

rounding using to-nearest rounding:
rint (2.3) = 2.0
rint (3.8) = 4.0
rint (-2.3) = -2.0
rint (-3.8) = -4.0

5、lrint:使用fegetround 得到的取整方向将x取整为整数
函数声明:

long int lrint (double x);
long int lrint (float x);
long int lrint (long double x);
long int lrint (T x);    

6、llrint:使用fegetround 得到的取整方向将x取整为整数
函数声明:

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);   

7、nearbyint :使用fegetround 得到的取整方向将x取整为整数
函数声明:

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

8、trunc:向0取整
函数声明:

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

9、floor:向下取整
函数声明:

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

10、ceil:向上取整
函数声明:

 double ceil (double x);
      float ceil (float x);
long double ceil (long double x);
     double ceil (T x);  
/* round vs floor vs ceil vs trunc */
#include <stdio.h>      /* printf */
#include <math.h>       /* round, floor, ceil, trunc */

int main ()
{
  const char * format = "%.1f \t%.1f \t%.1f \t%.1f \t%.1f\n";
  printf ("value\tround\tfloor\tceil\ttrunc\n");
  printf ("-----\t-----\t-----\t----\t-----\n");
  printf (format, 2.3,round( 2.3),floor( 2.3),ceil( 2.3),trunc( 2.3));
  printf (format, 3.8,round( 3.8),floor( 3.8),ceil( 3.8),trunc( 3.8));
  printf (format, 5.5,round( 5.5),floor( 5.5),ceil( 5.5),trunc( 5.5));
  printf (format,-2.3,round(-2.3),floor(-2.3),ceil(-2.3),trunc(-2.3));
  printf (format,-3.8,round(-3.8),floor(-3.8),ceil(-3.8),trunc(-3.8));
  printf (format,-5.5,round(-5.5),floor(-5.5),ceil(-5.5),trunc(-5.5));
  return 0;
}

运行结果:

value	round	floor	ceil	trunc
-----	-----	-----	----	-----
2.3 	2.0 	2.0 	3.0 	2.0
3.8 	4.0 	3.0 	4.0 	3.0
5.5 	6.0 	5.0 	6.0 	5.0
-2.3 	-2.0 	-3.0 	-2.0 	-2.0
-3.8 	-4.0 	-4.0 	-3.0 	-3.0
-5.5 	-6.0 	-6.0 	-5.0 	-5.0
发布了60 篇原创文章 · 获赞 10 · 访问量 3713

猜你喜欢

转载自blog.csdn.net/sinat_18811413/article/details/104439816