remainder, remainderf, remainderl
來自 cppreference.com
定義於標頭檔案 <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 起) |
1-3) 計算浮點除法操作 x/y 的 IEEE 餘數。
4) 型別泛型宏:如果任何引數的型別為 long double,則呼叫
remainderl
。否則,如果任何引數具有整數型別或型別為 double,則呼叫 remainder
。否則,呼叫 remainderf
。此函式計算的除法操作 x/y 的 IEEE 浮點餘數精確值為 x - n * y,其中 n
是最接近精確值 x/y 的整數值。當 |n-x/y| = ½ 時,n
被選擇為偶數。
與 fmod() 不同,返回值不保證與 x 具有相同的符號。
如果返回值為 0,它將與 x 具有相同的符號。
目錄 |
[edit] 引數
x, y | - | 浮點值 |
[edit] 返回值
如果成功,返回如上定義的除法 x/y 的 IEEE 浮點餘數。
如果發生域錯誤,則返回實現定義的值 (支援 NaN 時返回 NaN)。
如果由於下溢導致範圍錯誤,則返回正確結果。
如果 y 為零,但未發生域錯誤,則返回零。
[edit] 錯誤處理
錯誤按 math_errhandling
中指定的方式報告。
如果 y 為零,則可能發生域錯誤。
如果實現支援 IEEE 浮點運算 (IEC 60559),
- 當前的舍入模式無效。
- 從不引發 FE_INEXACT,結果始終精確。
- 如果 x 是 ±∞ 且 y 不是 NaN,則返回 NaN 並引發 FE_INVALID。
- 如果 y 是 ±0 且 x 不是 NaN,則返回 NaN 並引發 FE_INVALID。
- 如果任一引數為 NaN,則返回 NaN。
[edit] 注意
POSIX 要求,如果 x 為無窮大或 y 為零,則會發生域錯誤。
fmod(而非 remainder
)對於將浮點型別靜默封裝到無符號整數型別很有用:(0.0 <= (y = fmod(rint(x), 65536.0)) ? y : 65536.0 + y) 位於範圍 [
-0.0,
65535.0]
,這對應於 unsigned short,而 remainder(rint(x), 65536.0) 位於範圍 [
-32767.0,
+32768.0]
,這超出了 signed short 的範圍。
[edit] 示例
執行此程式碼
#include <fenv.h> #include <math.h> #include <stdio.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
[edit] 參考
- C23 標準 (ISO/IEC 9899:2024)
- 7.12.10.2 The remainder functions (p: TBD)
- 7.25 型別通用數學 <tgmath.h> (p: TBD)
- F.10.7.2 The remainder functions (p: TBD)
- C17 標準 (ISO/IEC 9899:2018)
- 7.12.10.2 The remainder functions (p: 185-186)
- 7.25 型別通用數學 <tgmath.h> (p: 272-273)
- F.10.7.2 The remainder functions (p: 385)
- C11 標準 (ISO/IEC 9899:2011)
- 7.12.10.2 The remainder functions (p: 254-255)
- 7.25 型別通用數學 <tgmath.h> (p: 373-375)
- F.10.7.2 The remainder functions (p: 529)
- C99 標準 (ISO/IEC 9899:1999)
- 7.12.10.2 The remainder functions (p: 235)
- 7.22 型別通用數學 <tgmath.h> (p: 335-337)
- F.9.7.2 The remainder functions (p: 465)
[edit] 另見
(C99) |
計算整數除法的商和餘數 (函式) |
(C99)(C99) |
計算浮點除法運算的餘數 (函式) |
(C99)(C99)(C99) |
計算帶符號餘數以及除法運算的最後三位 (函式) |
C++ 文件 關於 remainder
|