Namespaces
Variants

modf, modff, modfl

From cppreference.net
< c ‎ | numeric ‎ | math
Common mathematical functions
Functions
Basic operations
(C99)
(C99)
(C99)
(C99) (C99) (C99) (C23)
Maximum/minimum operations
Exponential functions
Power functions
Trigonometric and hyperbolic functions
Nearest integer floating-point
(C99) (C99) (C99)
(C23) (C23) (C23) (C23)
Floating-point manipulation
Narrowing operations
(C23)
(C23)
(C23)
(C23)
(C23)
(C23)
Quantum and quantum exponent
Decimal re-encoding functions
Total order and payload functions
Classification
Error and gamma functions
(C99)
(C99)
(C99)
(C99)
Types
Macro constants
Special floating-point values
Arguments and return values
Error handling
Fast operation indicators
ヘッダーで定義 <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 が指すオブジェクトに格納されます。

目次

翻訳のポイント: - 「Contents」→「目次」に翻訳 - HTMLタグ、属性、クラス名は一切変更せず保持 - C++関連の用語(Parameters、Return value、Error handling、Notes、Example、References、See also)は原文のまま保持 - 数値や構造は完全に維持 - プロフェッショナルな技術文書としての正確性を確保

パラメータ

arg - 浮動小数点値
iptr - 整数部を格納する浮動小数点値へのポインタ

戻り値

エラーが発生しない場合、 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);
}

#include <float.h>
#include <math.h>
#include <stdio.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

参考文献

  • C23規格 (ISO/IEC 9899:2024):
  • 7.12.6.12 modf関数群 (p: TBD)
  • F.10.3.12 modf関数群 (p: TBD)
  • C17規格 (ISO/IEC 9899:2018):
  • 7.12.6.12 modf関数群 (p: TBD)
  • F.10.3.12 modf関数群 (p: TBD)
  • 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関数

関連項目

(C99) (C99) (C99)
指定された値の絶対値より大きくない最も近い整数に丸める
(関数)