modf, modff, modfl
提供: cppreference.com
<tbody>
</tbody>
| ヘッダ <math.h> で定義
|
||
float modff( float arg, float* iptr ); |
(1) | (C99以上) |
double modf( double arg, double* iptr ); |
(2) | |
long double modfl( long double arg, long double* iptr ); |
(3) | (C99以上) |
1-3) 指定された浮動小数点値
arg を整数部と小数部に分解します。 各々は arg と同じ型および符号を持ちます。 (浮動小数点形式の) 整数部は iptr の指すオブジェクトに格納されます。引数
| arg | - | 浮動小数点値 |
| iptr | - | 整数部を格納する浮動小数点値を指すポインタ |
戻り値
エラーが発生しなければ、 arg と同じ符号を持つ arg の小数部が返されます。 整数部は 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);
}
例
Run this code
#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 standard (ISO/IEC 9899:2011):
- 7.12.6.12 The modf functions (p: 246-247)
- F.10.3.12 The modf functions (p: 523)
- C99 standard (ISO/IEC 9899:1999):
- 7.12.6.12 The modf functions (p: 227)
- F.9.3.12 The modf functions (p: 460)
- C89/C90 standard (ISO/IEC 9899:1990):
- 4.5.4.6 The modf function
関連項目
(C99)(C99)(C99) |
指定された値より絶対値が大きくない最も近い整数に丸めます (関数) |
modf の C++リファレンス
| |