c语言两个浮点数相加
As we know that modules also known as the remainder of the two numbers can be found using the modulus (%) operator which is an arithmetic operator in C/C++. The modules operator works with integer values i.e. modulus operator is used to find the remainder of two integer numbers. If the numbers are float or double the the modulus operators doesn't work.
我们知道,也可以使用模数 ( % )运算符(也就是C / C ++中的算术运算符)找到也称为两个数的余数的模块 。 模块运算符使用整数值,即模数运算符用于查找两个整数的余数。 如果数字为float或double ,则模运算符不起作用。
Consider the below example,
考虑下面的示例,
#include <stdio.h>
int main()
{
float x = 10.23f;
float y = 3.1f;
float result = x % y;
printf("result = %f\n", result);
return 0;
}
Output:
输出:
main.c: In function ‘main’:
main.c:8:22: error: invalid operands to binary % (have ‘float’ and ‘float’)
float result = x % y;
^
See the output – it says that invalid operands to modulus operator.
看到输出–它表示模数运算符的无效操作数。
Then, how to find the remainder/modulus of two float or double numbers?
然后, 如何找到两个浮点数或双数的余数/模数?
There is a library function remainder(), declared in math.h. It can be used to find the modules of float, double, and integer (also) numbers.
有一个库函数restder() ,在math.h中声明。 它可用于查找float , double和integer(也)数字的模块。
The remainder() function accepts two parameters and returns the remainder.
restder()函数接受两个参数并返回剩余的值。
Syntax:
句法:
remainder(x, y);
Here, x and y are the floating-point or integral values.
此处, x和y是浮点或整数值。
Example 1:
范例1:
#include <stdio.h>
#include <math.h>
int main()
{
float x = 10.23f;
float y = 3.1f;
float result = remainder(x, y);
printf("result = %f\n", result);
return 0;
}
Output:
输出:
result = 0.930000
Example 2:
范例2:
#include <stdio.h>
#include <math.h>
int main()
{
// integer numbers
int a = 10;
int b = 3;
// float numbers
float m = 10.23f;
float n = 3.1f;
// double numbers
double x = 123456789.10;
double y = 1233.1;
printf("remainder(%d,%d) = %lf\n", a, b, remainder(a, b));
printf("remainder(%f,%f) = %lf\n", m, n, remainder(m, n));
printf("remainder(%lf,%lf) = %lf\n", x, y, remainder(x, y));
return 0;
}
Output:
输出:
remainder(10,3) = 1.000000
remainder(10.230000,3.100000) = 0.930000
remainder(123456789.100000,1233.100000) = 50.200000
翻译自: https://www.includehelp.com/c/modulus-of-two-float-or-double-numbers-in-c-language.aspx
c语言两个浮点数相加