C
数值 | Numerics

modfl

modf, modff, modfl

在头文件中定义
float modff( float arg, float* iptr (1)(since C99)
double modf( double arg, double* iptr (2)
long double modfl( long double arg, long double* iptr (3)(since C99)

1-3)将给定的浮点值分解arg为整数和小数部分,每部分具有相同的类型和符号arg。整数部分(浮点格式)存储在指向的对象中iptr

参数

arg-浮点值
iptr-指向浮点值来存储整数部分

返回值

如果没有错误发生,返回与小数部分x相同的符号x。积分部分被放入到所指的值中iptr

返回的值和存储的值的和*iptr给出arg(允许舍入)。

错误处理

此函数不受在math_errhandling中指定的任何错误的影响。

如果实现支持IEEE浮点运算(IEC 60559),

  • 如果arg是±0,则返回±0,并存储±0 *iptr

笔记

该函数的行为如同执行如下:

double modf(double value, double *iptr) { #pragma STDC FENV_ACCESS ON int save_round = fegetround( fesetround(FE_TOWARDZERO *iptr = std::nearbyint(value fesetround(save_round return copysign(isinf(value) ? 0.0 : value - (*iptr), value }

Example

#include <stdio.h> #include <math.h> #include <float.h> int main(void) { double f = 123.45; printf("Given the number %.2f or %a in hex,\n", f, f double f3; double f2 = modf(f, &f3 printf("modf() makes %.2f + %.2f\n", f3, f2 int i; f2 = frexp(f, &i printf("frexp() makes %f * 2^%d\n", f2, i i = ilogb(f printf("logb()/ilogb() make %f * %d^%d\n", f/scalbn(1.0, i), FLT_RADIX, i // special values f2 = modf(-0.0, &f3 printf("modf(-0) makes %.2f + %.2f\n", f3, f2 f2 = modf(-INFINITY, &f3 printf("modf(-Inf) makes %.2f + %.2f\n", f3, f2 }

可能的输出:

Given the number 123.45 or 0x1.edccccccccccdp+6 in hex, modf() makes 123.00 + 0.45 frexp() makes 0.964453 * 2^7 logb()/ilogb() make 1.92891 * 2^6 modf(-0) makes -0.00 + -0.00 modf(-Inf) makes -INF + -0.00

参考

  • C11标准(ISO / IEC 9899:2011):