首页 > 文章列表 > 使用C语言计算两个浮点数或双精度数的模数

使用C语言计算两个浮点数或双精度数的模数

C语言;浮点数;模数
353 2023-08-19

在这里,我们将看到如何在C中获取两个浮点或双精度类型数据的模数。模数基本上是找到余数。为此,我们可以使用C中的remainder()函数。remainder()函数用于计算分子/分母的浮点余数。

因此,remainder(x, y)将如下所示。

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

The rquote is the value of x/y. This is rounded towards the nearest integral value. This function takes two arguments of type double, float, long double, and returns the remainder of the same type, that was given as argument. The first argument is numerator, and the second argument is the denominator.

Example

#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