函数族定义---四舍五入与余数函数
ceil---返回不小于x的最小整数
double ceil (double x);
代码示例:
printf ( "ceil of 2.3 is %.1f\n", ceil(2.3) );
printf ( "ceil of 3.8 is %.1f\n", ceil(3.8) );
printf ( "ceil of -2.3 is %.1f\n", ceil(-2.3) );
printf ( "ceil of -3.8 is %.1f\n", ceil(-3.8) );
//测试结果:
ceil of 2.3 is 3.0
ceil of 3.8 is 4.0
ceil of -2.3 is -2.0
ceil of -3.8 is -3.0
floor---返回不大于x的最大整数
double floor (double x);
代码示例:
printf ( "floor of 2.3 is %.1lf\n", floor (2.3) );
printf ( "floor of 3.8 is %.1lf\n", floor (3.8) );
printf ( "floor of -2.3 is %.1lf\n", floor (-2.3) );
printf ( "floor of -3.8 is %.1lf\n", floor (-3.8) );
//测试结果:
floor of 2.3 is 2.0
floor of 3.8 is 3.0
floor of -2.3 is -3.0
floor of -3.8 is -4.0
fmod---返回浮点数求余的结果
double fmod (double numer , double denom);
fmod = numer - tquot * denom,其中tquot = number/denom; 代码示例:
printf ( "fmod of 5.3 / 2 is %f\n", fmod (5.3,2) );
printf ( "fmod of 18.5 / 4.2 is %f\n", fmod (18.5,4.2) );
//测试结果:fmod of 5.3 / 2 is 1.300000
fmod of 18.5 / 4.2 is 1.700000
注意第二个计算中实际上4.2*4=16.8,这时剩下的18.5-16.8=1.7,不够做除法,当做余数返回
trunc---返回最接近x且绝对值不比x绝对值大的整数
double trunc ( double x);
代码示例:
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));
//测试结果:
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
可以看到trunc函数实际上是取整数部分,忽略小数部分
round---返回四舍五入的最近整数
double round (double x);
例子同上一个
lround---返回四舍五入的最近整数,以long int返回
long int lround (double x);
llround---返回四舍五入的最近整数,以long long int返回
long long int llround (double x);
rint---使用fegetround函数中的浮点数规则进行舍入
double rint (double x);
该函数可能接收FE_INEXACT的异常 fegetround的使用参考### fegetround---读取舍入信息 代码示例:
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) );
//测试结果
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
lrint---使用fegetround函数中的浮点数规则进行舍入,返回long int
long int lrint (double x);
llrint---使用fegetround函数中的浮点数规则进行舍入,返回long long int
long long int llrint (double x);
nearbyint---使用fegetround函数中的浮点数规则进行舍入
double nearbyint (double x);
该函数不会接收FE_INEXACT的异常,与rint有区别
remainder---返回浮点数除法的余数,但是这里的商会先四舍五入到最近的整数,然后再计算余数
double remainder (double numer , double denom);
与函数fmod相似,但又有不同,来看一看相似的例子:
printf ( "remainder of 5.3 / 2 is %f\n", remainder (5.3,2) );
printf ( "remainder of 18.5 / 4.2 is %f\n", remainder (18.5,4.2) );
//测试结果
remainder of 5.3 / 2 is -0.700000
remainder of 18.5 / 4.2 is 1.700000
其中第一个计算5.3/2 = 2.65,所以商舍入到整数3,然后计算余数5.3-2*3 = -0.7
remquo---返回浮点数除法的余数
double remquo (double numer , double denom , int* quot);
操作与remainder类似,但是会将商存储在quot中,例子如下:
double numer = 10.3;
double denom = 4.5;
int quot;
double result = remquo (numer,denom,");
printf ("numerator: %f\n", numer);
printf ("denominator: %f\n", denom);
printf ("remainder: %f\n", result);
printf ("quotient: %d\n", quot);
//测试结果
numerator: 10.300000
denominator: 4.500000
remainder: 1.300000
quotient: 2
浮点操作函数
copysign---返回一个数,它的绝对值等于x,符号与y相同
double copysign (double x , double y);
示例:
printf ("copysign ( 10.0,-1.0) = %f\n", copysign( 10.0,-1.0));
printf ("copysign (-10.0,-1.0) = %f\n", copysign(-10.0,-1.0));
printf ("copysign (-10.0, 1.0) = %f\n", copysign(-10.0, 1.0));
//测试结果copysign ( 10.0,-1.0) = -10.0
copysign (-10.0,-1.0) = -10.0
copysign (-10.0, 1.0) = 10.0
nan---返回一个double类型的NAN数
double nan (const char* tagp);
默认传递""或者"NAN"都可以。
nextafter---返回指定数x在指定数y方向(正数正方向,负数负方向)上所能表示的下一个数
double nextafter (double x , double y );
示例:
printf ("first representable value greater than zero: %e\n", nextafter(0.0,1.0));
printf ("first representable value less than zero: %e\n", nextafter(0.0,-1.0));
//测试结果
first representable value greater than zero: 4.940656e-324
first representable value less than zero: -4.940656e-324
可以看到,大于0的下一个可以表示的数为4.940656e-324,小于0的为-4.940656e-324
nexttoward---与nextafter相同,但y更精确,使用了long double类型
double nexttoward (double x , long double y);
示例:
printf ("first representable value greater than zero: %e\n", nexttoward(0.0,1.0L));标签:头文件,floor,double,08,ceil,源码,printf,2.3,3.8 From: https://blog.51cto.com/u_15830688/5907705
printf ("first representable value less than zero: %e\n", nexttoward(0.0,-1.0L));
//测试结果
first representable value greater than zero: 4.940656e-324
first representable value less than zero: -4.940656e-324