- cmath[meta header]
- std[meta namespace]
- function[meta id-type]
- cpp11[meta cpp]
namespace std {
double remainder(double x, double y);
float remainder(float x, float y);
long double remainder(long double x, long double y);
Integral remainder(Integral x, Integral y);
float remainderf(float x, float y); // C++17 から
long double remainderl(long double x, long double y); // C++17 から
}- Integral[italic]
浮動小数点数の剰余を求める。
整数に対する剰余は%演算子で求められるが、浮動小数点数に対しては本関数を使用する必要がある。
IEC 60559で要求されたx REM yを計算して返す。
yがゼロである場合、定義域エラーを発生させるかゼロを返すかは、実装定義となる。定義域エラーが発生した際の挙動については、<cmath> を参照。
- 本関数は、C99 の規格にある
remainder(より正確にはmath.hヘッダのremainder、remainderf、remainderlの 3 つ。それぞれ C++ のdouble、float、long doubleバージョンに相当)と等価である。 - IEC 60559で要求された
x REM yの計算とは以下のようなものであり、全ての実装に適用できる。- 「y≠0である場合、剰余r = x REM yは、丸めモードに関係なく数学的な関係r = x - nyによって定義される。ここで、nはx/yの正確な値に最も近い整数である。| n - x/y | = 1/2ならば、nは常に偶数である。したがって、剰余は常に正確である。r = 0の場合、その符号はxの符号とする」
#include <iostream>
#include <cmath>
void test(double x, double y)
{
std::cout << "remainder(" << x << ", " << y << ") = " << std::remainder(x, y) << std::endl;
}
int main()
{
test(5.0, 2.0);
test(6.0, 4.0);
test(6.3, 3.0);
test(6.3, -3.0);
test(-6.3, 3.0);
test(-6.3, -3.0);
test(6.3, 3.15);
test(6.0, 2.0);
}- std::remainder[color ff0000]
remainder(5, 2) = 1
remainder(6, 4) = -2
remainder(6.3, 3) = 0.3
remainder(6.3, -3) = 0.3
remainder(-6.3, 3) = -0.3
remainder(-6.3, -3) = -0.3
remainder(6.3, 3.15) = 0
remainder(6, 2) = 0
- C++11
- Clang, C++11 mode: 3.0
- GCC, C++11 mode: 4.3
- ICC: ??
- Visual C++: ??