Namespaces
Variants

fmod, fmodf, fmodl

From cppreference.net
< c ‎ | numeric ‎ | math
Common mathematical functions
Functions
Basic operations
fmod
(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 fmodf ( float x, float y ) ;
(1) (C99以降)
double fmod ( double x, double y ) ;
(2)
long double fmodl ( long double x, long double y ) ;
(3) (C99以降)
ヘッダーで定義 <tgmath.h>
#define fmod( x, y )
(4) (C99以降)
1-3) 除算演算 x / y の浮動小数点剰余を計算します。
4) 型総称マクロ: いずれかの引数が型 long double を持つ場合、 fmodl が呼び出される。そうでなく、いずれかの引数が整数型または型 double を持つ場合、 fmod が呼び出される。それ以外の場合、 fmodf が呼び出される。

この関数によって計算される除算演算 x / y の浮動小数点剰余は、厳密に値 x - n * y となります。ここで n x / y の小数部が切り捨てられた値です。

返される値は x と同じ符号を持ち、その絶対値は y 以下となります。

目次

翻訳の説明: - 「Contents」を「目次」に翻訳しました - HTMLタグ、属性、
タグ内のテキストは翻訳していません
- C++固有の用語(Parameters、Return value、Error handling、Notes、Example、References、See also)は原文のまま保持しました
- 元のフォーマットと構造を完全に保持しています

パラメータ

x, y - 浮動小数点値

戻り値

成功した場合、上記で定義された除算 x / y の浮動小数点剰余を返します。

定義域エラーが発生した場合、実装定義の値が返されます(NaNがサポートされている場合はNaN)。

アンダーフローによる範囲エラーが発生した場合、正しい結果(丸め後)が返されます。

エラー処理

エラーは math_errhandling で指定された通りに報告されます。

y がゼロの場合、定義域エラーが発生する可能性があります。

IEEE浮動小数点演算(IEC 60559)がサポートされている実装の場合:

  • x が ±0 で y が非ゼロの場合、±0 が返される。
  • x が ±∞ で y が NaN でない場合、NaN が返され FE_INVALID が発生する。
  • y が ±0 で x が NaN でない場合、NaN が返され FE_INVALID が発生する。
  • y が ±∞ で x が有限値の場合、 x が返される。
  • いずれかの引数が NaN の場合、NaN が返される。

注記

POSIXでは x が無限大または y がゼロの場合、定義域エラーが発生することが要求されています。

fmod は有用ですが、 remainder はそうではありません。浮動小数点型から符号なし整数型へのサイレントラッピングを行う場合に有用です: ( 0.0 <= ( y = fmod ( rint ( x ) , 65536.0 ) ) ? y : 65536.0 + y ) は範囲 [ - 0.0 , 65535.0 ] 内に収まり、これは unsigned short に対応します。しかし remainder ( rint ( x ) , 65536.0 ) は範囲 [ - 32767.0 , + 32768.0 ] 内に収まり、これは signed short の範囲外です。

double バージョンの fmod は以下のように実装されているかのように動作します:

double fmod(double x, double y)
{
#pragma STDC FENV_ACCESS ON
    double result = remainder(fabs(x), (y = fabs(y)));
    if (signbit(result))
        result += y;
    return copysign(result, x);
}

#include <fenv.h>
#include <math.h>
#include <stdio.h>
// #pragma STDC FENV_ACCESS ON
int main(void)
{
    printf("fmod(+5.1, +3.0) = %.1f\n", fmod(5.1, 3));
    printf("fmod(-5.1, +3.0) = %.1f\n", fmod(-5.1, 3));
    printf("fmod(+5.1, -3.0) = %.1f\n", fmod(5.1, -3));
    printf("fmod(-5.1, -3.0) = %.1f\n", fmod(-5.1, -3));
    // 特殊値
    printf("fmod(+0.0, 1.0) = %.1f\n", fmod(0, 1));
    printf("fmod(-0.0, 1.0) = %.1f\n", fmod(-0.0, 1));
    printf("fmod(+5.1, Inf) = %.1f\n", fmod(5.1, INFINITY));
    // エラー処理
    feclearexcept(FE_ALL_EXCEPT);
    printf("fmod(+5.1, 0) = %.1f\n", fmod(5.1, 0));
    if (fetestexcept(FE_INVALID))
        puts("    FE_INVALID raised");
}

出力例:

fmod(+5.1, +3.0) = 2.1
fmod(-5.1, +3.0) = -2.1
fmod(+5.1, -3.0) = 2.1
fmod(-5.1, -3.0) = -2.1
fmod(+0.0, 1.0) = 0.0
fmod(-0.0, 1.0) = -0.0
fmod(+5.1, Inf) = 5.1
fmod(+5.1, 0) = nan
    FE_INVALID raised

参考文献

  • C23規格 (ISO/IEC 9899:2024):
  • 7.12.10.1 fmod関数群 (p: TBD)
  • 7.25 総称数学 <tgmath.h> (p: TBD)
  • F.10.7.1 fmod関数群 (p: TBD)
  • C17規格 (ISO/IEC 9899:2018):
  • 7.12.10.1 fmod関数群 (p: 185)
  • 7.25 総称数学 <tgmath.h> (p: 274-275)
  • F.10.7.1 fmod関数群 (p: 385)
  • C11規格 (ISO/IEC 9899:2011):
  • 7.12.10.1 fmod関数群 (p: 254)
  • 7.25 総称数学 <tgmath.h> (p: 373-375)
  • F.10.7.1 fmod関数群 (p: 528)
  • C99規格 (ISO/IEC 9899:1999):
  • 7.12.10.1 fmod関数群 (p: 235)
  • 7.22 総称数学 <tgmath.h> (p: 335-337)
  • F.9.7.1 fmod関数群 (p: 465)
  • C89/C90標準 (ISO/IEC 9899:1990):
  • 4.5.6.4 fmod関数

関連項目

整数除算の商と余りを計算する
(関数)
浮動小数点除算操作の符号付き余りを計算する
(関数)
(C99) (C99) (C99)
符号付き余りと除算操作の下位3ビットを計算する
(関数)