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
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言的编程
C++
C#
MongoDB
MySQL
Javascript
PHP