std:: remainder, std:: remainderf, std:: remainderl
|
定義先ヘッダ
<cmath>
|
||
| (1) | ||
|
float
remainder
(
float
x,
float
y
)
;
double
remainder
(
double
x,
double
y
)
;
|
(C++23以前) | |
|
constexpr
/*floating-point-type*/
remainder
(
/*floating-point-type*/
x,
|
(C++23以降) | |
|
float
remainderf
(
float
x,
float
y
)
;
|
(2) |
(C++11以降)
(C++23以降 constexpr) |
|
long
double
remainderl
(
long
double
x,
long
double
y
)
;
|
(3) |
(C++11以降)
(C++23以降 constexpr) |
|
SIMD オーバーロード
(C++26以降)
|
||
|
定義先ヘッダ
<simd>
|
||
|
template
<
class
V0,
class
V1
>
constexpr
/*math-common-simd-t*/
<
V0, V1
>
|
(S) | (C++26以降) |
|
追加のオーバーロード
(C++11以降)
|
||
|
定義先ヘッダ
<cmath>
|
||
|
template
<
class
Integer
>
double remainder ( Integer x, Integer y ) ; |
(A) | (C++23以降 constexpr) |
std::remainder
のオーバーロードを提供します。
(C++23以降)
|
S)
SIMDオーバーロードは、
v_x
と
v_y
に対して要素ごとの
std::remainder
を実行します。
|
(C++26以降) |
|
A)
すべての整数型に対して追加のオーバーロードが提供されており、これらは
double
として扱われます。
|
(since C++11) |
この関数によって計算される除算操作 x / y のIEEE浮動小数点剰余は、厳密に値 x - quo * y であり、ここで値 quo は厳密な値 x / y に最も近い整数値である。 |quo - x / y| = ½ の場合、値 quo は偶数となるように選択される。
std::fmod とは対照的に、返される値が x と同じ符号を持つことは保証されません。
返される値がゼロの場合、それは 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 がゼロの場合、定義域エラーが発生することが要求されています。
std::fmod
は浮動小数点型から符号なし整数型へのサイレントラッピングに有用ですが、
std::remainder
はそうではありません:
(
0.0
<=
(
y
=
std::
fmod
(
std::
rint
(
x
)
,
65536.0
)
)
)
?
y
:
65536.0
+
y
は範囲
[
-
0.0
,
65535.0
]
内に収まり、これは
unsigned
short
に対応します。しかし
std
::
remainder
(
std::
rint
(
x
)
,
65536.0
)
は範囲
[
-
32767.0
,
+
32768.0
]
内に収まり、これは
signed
short
の範囲外です。
追加のオーバーロードは (A) と完全に同一である必要はありません。それらは、第一引数 num1 と第二引数 num2 について以下を保証するのに十分であればよいのです:
|
(C++23まで) |
|
num1
と
num2
が算術型を持つ場合、
std
::
remainder
(
num1, num2
)
は
std
::
remainder
(
static_cast
<
/*common-floating-point-type*/
>
(
num1
)
,
最も高いランクとサブランクを持つ浮動小数点型が存在しない場合、 オーバーロード解決 は提供されたオーバーロードから使用可能な候補を生成しない。 |
(C++23以降) |
例
#include <cfenv> #include <cmath> #include <iostream> // #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'; // special values std::cout << "remainder(-0.0, 1.0) = " << std::remainder(-0.0, 1) << '\n' << "remainder(5.1, Inf) = " << std::remainder(5.1, INFINITY) << '\n'; // error handling 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ビット
(関数) |
|
C documentation
for
remainder
|
|