函式
<cmath> <ctgmath>

remainder

     double remainder  (double numer     , double denom);      float remainderf (float numer      , float denom);long double remainderl (long double numer, long double denom);
     double remainder (double numer     , double denom);      float remainder (float numer      , float denom);long double remainder (long double numer, long double denom);     double remainder (Type1 numer      , Type2 denom);       // additional overloads
計算餘數 (IEC 60559)
返回 numer/denom 的浮點餘數(四捨五入到最近的值)。

remainder = numer - rquot * denom

其中 rquotnumer/denom 的結果,向最近的整數值舍入(中間情況則向偶數舍入)。

一個類似的函式 fmod 返回相同的結果,但其商是截斷的(向零舍入)。
函式 remquo 的行為與此函式相同,但它還額外提供了對所使用的中間商值的訪問。

標頭檔案 <tgmath.h> 提供了此函式的型別通用宏版本。
此標頭檔案 (<cmath>) 中為其他算術型別 (Type1Type2) 的組合提供了附加過載:這些過載在計算前會有效地將其引數強制轉換為 double,除非至少有一個引數的型別是 long double(在這種情況下,兩個引數都會被強制轉換為 long double)。

引數

numer
商的分子值。
denom
商的分母值。

返回值

引數相除的餘數。
如果餘數為零,其符號應與 numer 的符號相同。
如果 denom 為零,該函式可能返回零或導致一個定義域錯誤(取決於庫的實現)。

如果發生定義域錯誤
- 並且 math_errhandling 設定了 MATH_ERRNO:全域性變數 errno 會被設定為 EDOM
- 並且 math_errhandling 設定了 MATH_ERREXCEPT:將引發 FE_INVALID

示例

1
2
3
4
5
6
7
8
9
10
/* remainder example */
#include <stdio.h>      /* printf */
#include <math.h>       /* remainder */

int main ()
{
  printf ( "remainder of 5.3 / 2 is %f\n", remainder (5.3,2) );
  printf ( "remainder of 18.5 / 4.2 is %f\n", remainder (18.5,4.2) );
  return 0;
}

輸出

remainder of 5.3 / 2 is -0.700000
remainder of 18.5 / 4.2 is 1.700000


另見