使用C的两个浮点数或双数的模数
在这里,我们将看到如何在C中获得两个浮点型或双精度型数据的模数。模数基本上是在寻找余数。为此,我们可以使用remainder()C语言中的remainder()函数。该函数用于计算分子/分母的浮点余数。
因此,余数(x,y)将如下所示。
remainder(x, y) = x – rquote * y
rquote是x/y的值。这四舍五入为最接近的整数值。此函数接受double,float,longdouble类型的两个参数,并返回相同类型的剩余部分,作为参数给出。第一个参数是分子,第二个参数是分母。
示例
#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\n",x,y, res);
x = -34.50;
y = 4.0;
res = remainder(x, y);
printf("Remainder of %lf/%lf is: %lf\n",x,y, res);
x = 65.23;
y = 0;
res = remainder(x, y);
printf("Remainder of %lf/%lf is: %lf\n",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
热门推荐
10 新人红包祝福语简短精辟
11 幼师给毕业祝福语简短
12 生日高考祝福语妹妹简短
13 下飞机祝福语简短英文
14 元宵发客户祝福语简短
15 爸妈金婚文案祝福语简短
16 新郎朋友红包祝福语简短
17 虎年五一祝福语大全简短
18 出阁宴祝福语姐姐简短