c语言两个浮点数相加_C语言中两个浮点数或双精度数的模数
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.930000Example 2:
范例2:
#include <stdio.h> #include <math.h>int main() {// integer numbersint a = 10;int b = 3;// float numbersfloat m = 10.23f;float n = 3.1f;// double numbersdouble 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語言兩個浮點數相加
總結
以上是生活随笔為你收集整理的c语言两个浮点数相加_C语言中两个浮点数或双精度数的模数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java math 类_Java Mat
- 下一篇: 保姆级教程,终于搞懂脏读、幻读和不可重复