C 語言中的浮點或雙精度數取模


在這裡,我們將看到如何求 C 語言中兩個浮點或雙精度型別資料的模。模基本上是求餘數。為此,我們可以使用 C 語言中的 remainder() 函式。remainder() 函式用來計算分子/分母的浮點數餘數。

所以,remainder(x, y) 將如下所示。

remainder(x, y) = x – rquote * y

rquote 是 x/y 的值。它的值會被舍入到最近的整數。此函式採用兩個 double、float、long double 型別的引數,並返回與引數相同的型別,也就是傳遞的引數的型別。第一個引數是分子,第二個引數是分母。

示例

#include <stdio.h>
#include <math.h>
main() {
   double x = 14.5, y = 4.1;
   double res = remainder(x, y);
   printf("Remainder of %lf/%lf is: %lf
",x,y, res);    x = -34.50;    y = 4.0;    res = remainder(x, y);    printf("Remainder of %lf/%lf is: %lf
",x,y, res);    x = 65.23;    y = 0;    res = remainder(x, y);    printf("Remainder of %lf/%lf is: %lf
",x,y, res); }

輸出

Remainder of 14.500000/4.100000 is: -1.900000
Remainder of -34.500000/4.000000 is: 1.500000
Remainder of 65.230000/0.000000 is: -1.#IND00

更新於:2019-07-30

7 千+ 瀏覽次數

開啟您的 職業生涯

透過完成本課程獲取認證

開始
廣告
© . All rights reserved.