std::remainder, std::remainderf, std::remainderl
| ヘッダ <cmath> で定義
|
||
| float remainder ( float x, float y ); float remainderf( float x, float y ); |
(1) | (C++11以上) |
| double remainder ( double x, double y ); |
(2) | (C++11以上) |
| long double remainder ( long double x, long double y ); long double remainderl( long double x, long double y ); |
(3) | (C++11以上) |
| 昇格後の型 remainder ( 算術型1 x, 算術型2 y ); |
(4) | (C++11以上) |
この関数によって計算される除算 x/y の IEEE 浮動小数点剰余は、正確に値 x - n*y です。 ただし値 n は正確な値 x - n*y に最も近い整数値です。 |n-x/y| = ½ のとき、値 n は偶数になるように選ばれます。
std::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 がゼロの場合、定義域エラーが発生することを要求しています。
std::fmod は浮動小数点型の符号なし整数型へのサイレントなラッピングを行うのに便利です (しかし std::remainder はそうではありません)。 (0.0 <= (y = std::fmod( std::rint(x), 65536.0 )) ? y : 65536.0 + y) は unsigned short に対応する [-0.0 .. 65535.0] の範囲内になりますが、 std::remainder(std::rint(x), 65536.0) は signed short の範囲外である [-32767.0, +32768.0] の範囲内になります。
[編集] 例
#include <iostream> #include <cmath> #include <cfenv> #pragma STDC FENV_ACCESS ON int main() { std::cout << "remainder(+5.1, +3.0) = " << std::remainder(5.1,3) << '\n' << "remainder(-5.1, +3.0) = " << std::remainder(-5.1,3) << '\n' << "remainder(+5.1, -3.0) = " << std::remainder(5.1,-3) << '\n' << "remainder(-5.1, -3.0) = " << std::remainder(-5.1,-3) << '\n'; // 特殊な値 std::cout << "remainder(-0.0, 1.0) = " << std::remainder(-0.0, 1) << '\n' << "remainder(5.1, Inf) = " << std::remainder(5.1, INFINITY) << '\n'; // エラー処理 std::feclearexcept(FE_ALL_EXCEPT); std::cout << "remainder(+5.1, 0) = " << std::remainder(5.1, 0) << '\n'; if(fetestexcept(FE_INVALID)) std::cout << " FE_INVALID raised\n"; }
出力例:
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
remainder(5.1, Inf) = 5.1
remainder(+5.1, 0) = -nan
FE_INVALID raised[編集] 関連項目
| (C++11) |
整数除算の商と余りを計算します (関数) |
| (C++11)(C++11) |
浮動小数点除算の余りを計算します (関数) |
| (C++11)(C++11)(C++11) |
除算の下位3ビットと符号付きの余りを計算します (関数) |
| remainder の C言語リファレンス
| |

