十进制数字的数字实现是浮点数。在C ++编程语言中,浮点数的大小为32位。还有一些浮点操作函数可用于浮点数。在这里,我们介绍了一些浮点操作函数。
fmod()
对浮点数进行操作的函数将返回该方法传递的参数的除法余数。
#include <iostream> #include <cmath> using namespace std; int main() { float a, b, rem; a = 23.4; b = 4.1; rem = fmod(a,b); cout<<"The value of fmod( "<<a<<" , "<<b<<" ) = "<<rem; }
The value of fmod( 23.4 , 4.1 ) = 2.9
在其余的()函数做同样的工作FMOD功能。并返回值之间的除法余数。此方法返回以数值表示的最小可能余数。它也可能是负面的。
#include <iostream> #include <cmath> using namespace std; int main() { float a, b, rem; a = 23.4; b = 4.1; rem = remainder(a,b); cout<<"The value of remainder( "<<a<<" , "<<b<<" ) = "<<rem; }
The value of remainder( 23.4 , 4.1 ) = -1.2
此方法返回商和传递的两个值的余数,并且它需要引用一个将具有商值的变量。因此,此方法将返回与余数函数和商的引用相同的余数。
#include <iostream> #include <cmath> using namespace std; int main() { float a, b, rem; int quo; a = 23.4; b = 4.1; rem = remquo(a,b,&quo); cout<<a<<" and "<<b<<" passed to the remquo() function gives the following output\n"; cout<<"The remainder is "<<rem<<endl; cout<<"The quotient is "<<quo; }
23.4 and 4.1 pass to the the remque() function gives the following output The reminder is -1.2 The quotient is 6
C的copysign函数返回一个带有其他变量符号的变量。返回的变量具有第一个变量的大小和第二个变量的符号。
#include <iostream> #include <cmath> using namespace std; int main(){ double a, b; a = 9.6; b = -3.5; cout<<"copysign function with inputs "<<a<<" and "<<b<<" is "<<copysign(a,b); }
Copysign function with inputs 9.6 and -3.5 is -9.6
从名称中可以看到fmin函数返回该函数的两个参数的最小值。返回类型是浮点数。
#include <iostream> #include <cmath> using namespace std; int main(){ double a, b; a = 43.5; b = 21.2; cout << "The smallest of "<<a<<" and "<<b<<" is "; cout << fmin(a,b)<<endl; }
The smallest of 43.5 and 21.2 is 21.2
fmax函数是C编程函数,它返回参数中两个数字中的最大数字。
#include <iostream> #include <cmath> using namespace std; int main(){ double a, b; a = 43.5; b = 21.2; cout << "The largest of "<<a<<" and "<<b<<" is "; cout << fmax(a,b)<<endl; }
The largest of 43.5 and 21.2 is 43.5
fdim()
C编程语言的函数返回作为函数参数发送的两个数字的绝对差。
#include <iostream> #include <cmath> using namespace std; int main(){ double a, b; a = 43.5; b = 21.2; cout << "The absolute difference of "<<a<<" and "<<b<<" is"; cout << fdim(a,b)<<endl; }
The absolute difference of 43.5 and 21.2 is 22.3
C的fma()函数返回为其提供的参数的乘法。该函数返回一个float并接受三个float参数。
#include <iostream> #include <cmath> using namespace std; int main(){ double a, b, c; a = 3.5; b = 2.4; c = 7.2; cout << "The multiplication of "<<a<<" , "<<b<<" and "<<c<<" is "; cout << fma(a,b,c)<<endl; }
The multiplication of 3.5 , 2.4 and 7.2 is 15.6
这些是对浮点数进行运算的所有功能。这些是cmath库中定义的函数