round.c

来自「语音CELP压缩解压源代码(C语音)」· C语言 代码 · 共 47 行

C
47
字号
/* This routine takes in a floating point number and rounds it to *//* the nearest integer. 					  */intround(afloat)float afloat;{int rounded_int;  /* this will truncate afloat */  rounded_int = afloat;  /* positive and negative numbers are handled differently */  if (afloat < 0)   {    /* if the fractional part is -.5 or less round down */    if (afloat - rounded_int <= -.5) rounded_int--;  }  else  {    /* if the fractional part is .5 or greater round up */    if (afloat - rounded_int >= .5) rounded_int++;  }          return(rounded_int);}#ifdef TEST#include <stdio.h>main(){    float x,y,z;    int i;    x = -2.000001;    y = -2.000000;    z = -1.999999;    for (i=0; i<16; i++) {        printf("%f -> %d     %f -> %d     %f -> %d\n",                x,round(x),y,round(y),z,round(z));        x = x + 0.25;        y = y + 0.25;        z = z + 0.25;        }    }#endif

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?