?
This document uses PHP Chinese website manual Release
在頭文件<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
為整數(shù)和小數(shù)部分,每部分具有相同的類型和符號arg
。整數(shù)部分(浮點格式)存儲在指向的對象中iptr
。
arg | - | 浮點值 |
---|---|---|
iptr | - | 指向浮點值來存儲整數(shù)部分 |
如果沒有錯誤發(fā)生,返回與小數(shù)部分x
相同的符號x
。積分部分被放入到所指的值中iptr
。
返回的值和存儲的值的和*iptr
給出arg
(允許舍入)。
此函數(shù)不受在math_errhandling中指定的任何錯誤的影響。
如果實現(xiàn)支持IEEE浮點運算(IEC 60559),
如果arg
是±0,則返回±0,并存儲±0 *iptr
。
如果arg
是±∞,返回±0,并存儲±∞ *iptr
。
如果arg
是NaN,則返回NaN,并存儲NaN *iptr
。
返回的值是精確的,當前舍入模式被忽略
該函數(shù)的行為如同執(zhí)行如下:
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函數(shù)(p:246-247)
F.10.3.12 modf函數(shù)(p:523)
C99標準(ISO / IEC 9899:1999):
7.12.6.12 modf函數(shù)(p:227)
F.9.3.12 modf函數(shù)(p:460)
C89 / C90標準(ISO / IEC 9899:1990):
4.5.4.6 modf函數(shù)