modf, modff, modfl
提供: cppreference.com
ヘッダ <math.h> で定義 | ||
float modff(float arg, float* iptr ); | (1) | (C99以上) |
double modf(double arg, double* iptr ); | (2) | |
longdouble modfl(longdouble arg, longdouble* 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 ONint save_round =fegetround();fesetround(FE_TOWARDZERO);*iptr = std::nearbyint(value);fesetround(save_round);returncopysign(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)