Namespaces
Variants

remainder, remainderf, remainderl

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

この関数によって計算される除算操作 x / y のIEEE浮動小数点剰余は、厳密に値 x - n * y となります。ここで値 n は、厳密な値 x / y に最も近い整数値です。 |n-x/y| = ½ の場合、値 n は偶数となるように選択されます。

fmod() とは対照的に、返される値は x と同じ符号を持つことが保証されていません。

返される値が 0 の場合、 x と同じ符号を持ちます。

目次

パラメータ

x, y - 浮動小数点値

戻り値

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

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

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

y がゼロであるが、定義域エラーが発生しない場合、ゼロが返されます。

エラーハンドリング

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

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

IEEE浮動小数点演算(IEC 60559)を実装がサポートしている場合、

  • 現在の 丸めモード は効果を持たない。
  • FE_INEXACT は決して発生せず、結果は常に正確である。
  • x が ±∞ かつ y が NaN でない場合、NaN が返され FE_INVALID が発生する。
  • y が ±0 かつ x が NaN でない場合、NaN が返され FE_INVALID が発生する。
  • いずれかの引数が 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 の範囲外となります。

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

出力:

remainder(+5.1, +3.0) = -0.9
remainder(-5.1, +3.0) = 0.9
remainder(+5.1, -3.0) = -0.9
remainder(-5.1, -3.0) = 0.9
remainder(+0.0, 1.0) = 0.0
remainder(-0.0, 1.0) = -0.0
remainder(+5.1, Inf) = 5.1
remainder(+5.1, 0) = -nan
    FE_INVALID raised

参考文献

  • C23規格 (ISO/IEC 9899:2024):
  • 7.12.10.2 剰余関数 (p: TBD)
  • 7.25 総称数学 <tgmath.h> (p: TBD)
  • F.10.7.2 剰余関数 (p: TBD)
  • C17規格 (ISO/IEC 9899:2018):
  • 7.12.10.2 remainder関数群 (p: 185-186)
  • 7.25 総称型数学 <tgmath.h> (p: 272-273)
  • F.10.7.2 remainder関数群 (p: 385)
  • C11規格 (ISO/IEC 9899:2011):
  • 7.12.10.2 剰余関数 (p: 254-255)
  • 7.25 総称型数学 <tgmath.h> (p: 373-375)
  • F.10.7.2 剰余関数 (p: 529)
  • C99標準 (ISO/IEC 9899:1999):
  • 7.12.10.2 剰余関数 (p: 235)
  • 7.22 総称数学 <tgmath.h> (p: 335-337)
  • F.9.7.2 剰余関数 (p: 465)

関連項目

整数除算の商と余りを計算する
(関数)
(C99) (C99)
浮動小数点除算演算の余りを計算する
(関数)
(C99) (C99) (C99)
符号付き余りと除算演算の下位3ビットを計算する
(関数)
C++ documentation for remainder