目录搜索
文字
分享

在头文件<math.h>中定义



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

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

  • 如果arg是NaN,则返回NaN,并存储NaN *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);}

#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.45frexp() makes 0.964453 * 2^7logb()/ilogb() make 1.92891 * 2^6modf(-0) makes -0.00 + -0.00modf(-Inf) makes -INF + -0.00

参考

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

    • 7.12.6.12 modf函数(p:246-247)

    • F.10.3.12 modf函数(p:523)

  • C99标准(ISO / IEC 9899:1999):

    • 7.12.6.12 modf函数(p:227)

    • F.9.3.12 modf函数(p:460)

  • C89 / C90标准(ISO / IEC 9899:1990):

    • 4.5.4.6 modf函数

上一篇:MATH_ERRNO下一篇:modff