
C 速查手冊
11.1.4 remainder()
math.h 的函數 (function) remainder() 回傳第一個參數 (parameter) 除以第二個參數中的浮點餘數,預設回傳值 (return value) 及參數的資料型態 (data type) 為 double ,另有 float 型態的 remainderf() , long double 型態的 remainderl() 。
remainder() 的函數原型 (prototype) 如下
| double remainder(double, double); |
| float remainderf(float, float); |
| long double remainderl(long double, long double); |
以下程式示範函數 remainder() 的使用
#include <stdio.h>
#include <math.h>
int main(void)
{
printf("%f\n", remainder(33, 23.5));
return 0;
}
/* 《程式語言教學誌》的範例程式
http://kaiching.org/
檔名:remainder1.c
功能:示範 math.h 中函數 remainder() 的使用
作者:張凱慶 */
編譯後執行,結果如下
| $ gcc remainder1.c |
| $ a.out |
| 9.500000 |
| $ |
使用 remainder() 須注意,因為計算方式的關係,導致有時候會計算出負的餘數值
#include <stdio.h>
#include <math.h>
int main(void)
{
double a = 10.0;
double b = 1.0;
while (b < 12) {
printf("%f\n", remainder(a, b));
b += 1;
}
return 0;
}
/* 《程式語言教學誌》的範例程式
http://kaiching.org/
檔名:remainder2.c
功能:示範 math.h 中函數 remainder() 的使用
作者:張凱慶 */
編譯後執行,結果如下
| $ gcc remainder2.c |
| $ a.out |
| 0.000000 |
| 0.000000 |
| 1.000000 |
| 2.000000 |
| 0.000000 |
| -2.000000 |
| 3.000000 |
| 2.000000 |
| 1.000000 |
| 0.000000 |
| -1.000000 |
| $ |
