remainder, remainderf, remainderl
ヘッダ <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以上) |
remainderl
が呼ばれます。 そうでなく、いずれかの引数が整数型または double 型の場合は remainder
が呼ばれます。 そうでなければ remainderf
が呼ばれます。この関数によって計算される除算 x/y の IEEE 浮動小数点剰余は、正確に値 x - n*y です。 ただし値 n
は正確な値 x - n*y に最も近い整数値です。 |n-x/y| = ½ のとき、値 n
は偶数になるように選ばれます。
fmod() と対照的に、戻り値が x
と同じ符号を持��ことは保証されません。
戻り値が 0
の場合、 x
と同じ符号を持ちます。
目次 |
[編集] 引数
x, y | - | 浮動小数点値 |
[編集] 戻り値
成���した場合、上で定義された通りの除算 x/y の IEEE 浮動小数点剰余を返します。
定義域エラーが発生した場合、処理系定義の値 (サポートされている場合は 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( std::rint(x), 65536.0 )) ? y : 65536.0 + y) は unsigned short に対応する [-0.0 .. 65535.0]
の範囲内になりますが、 remainder(std::rint(x), 65536.0) は signed short の範囲外である [-32767.0, +32768.0]
の範囲内になります。
[編集] 例
#include <stdio.h> #include <math.h> #include <fenv.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)); // special values printf("remainder(-0.0, 1.0) = %.1f\n", remainder(-0.0, 1)); printf("remainder(+5.1, Inf) = %.1f\n", remainder(5.1, INFINITY)); // error handling 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
[編集] 参考文献
- C11 standard (ISO/IEC 9899:2011):
- 7.12.10.2 The remainder functions (p: 254-255)
- 7.25 Type-generic math <tgmath.h> (p: 373-375)
- F.10.7.2 The remainder functions (p: 529)
- C99 standard (ISO/IEC 9899:1999):
- 7.12.10.2 The remainder functions (p: 235)
- 7.22 Type-generic math <tgmath.h> (p: 335-337)
- F.9.7.2 The remainder functions (p: 465)
[編集] 関連項目
(C99) |
整数除算の商と余りを計算します (関数) |
(C99)(C99) |
浮動小数点除算の余りを計算します (関数) |
(C99)(C99)(C99) |
除算の下位3ビットと符号付きの余りを計算します (関数) |
remainder の C++リファレンス
|