std::remainder, std::remainderf, std::remainderl
定義於標頭檔案 <cmath> |
||
(1) | ||
float remainder ( float x, float y ); double remainder ( double x, double y ); |
(直至 C++23) | |
constexpr /*浮點型別*/ remainder ( /*浮點型別*/ 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。
|
(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) |
帶符號餘數以及除法運算的最後三位 (函式) |
C 文件 為 remainder
|