C++ 浮点运算
与整数一样,C++11 引入了一些基本的内置函数,用于处理日常编程和竞争性编程所需的浮点数的简单数学计算。这些函数属于
1. fmod() :此函数用于返回其参数中提到的 2 个浮点数的余数(模数) 。计算的商被截断。
句法:
fmod(a,b); // a, b are 2 floating point numbers
例子:
C++
// C++ code to demonstrate working of fmod()
#include
#include
using namespace std;
// Driver Code
int main()
{
double a, b, c;
// Initializing values
a = 9.6;
b = 3.5;
// using fmod() to compute the remainder
// computes 2 as quotient (truncated)
// returns 2.6 as remainder
c = fmod(a, b);
cout << "The remainder computed using fmod() is : "
<< c;
cout << endl;
}
C++
// C++ code to demonstrate working of remainder()
#include
#include
using namespace std;
// Driver Code
int main()
{
double a, b, c;
// Initializing values
a = 9.6;
b = 3.5;
// using remainder() to compute the remainder
// computes 3 as quotient (rounded)
// returns -0.9 as remainder
c = remainder(a, b);
cout << "The remainder computed using remainder() is : "
<< c;
cout << endl;
}
CPP
// C++ code to demonstrate working of remquo()
#include
#include
using namespace std;
// Driver Code
int main()
{
double a, b, f;
int g;
// Initializing values
a = 9.6;
b = 3.5;
// using remquo() to return quotient and remainder
// quotient stored in g
f = remquo(a, b, &g);
cout << "The remainder part of " << a << "/" << b
<< " is : " << f;
cout << endl;
cout << "The quotient part of " << a << "/" << b
<< " is : " << g;
cout << endl;
}
C++
// C++ code to demonstrate working of copysign()
#include
#include
using namespace std;
// Driver Code
int main()
{
double a, b;
// Initializing values
a = 9.6;
b = -3.5;
// using copysign()
cout << "The value returned after copysigning is : ";
cout << copysign(a, b);
cout << endl;
}
CPP
// C++ code to demonstrate working of nextafter()
#include
#include
using namespace std;
// Driver Code
int main()
{
double a, b;
// Initializing values
a = -3.5;
b = 0.0;
// using nextafter() to compute next approximated value
cout << "The next value approximated is : ";
cout << nextafter(a, b);
}
C++
// C++ code to demonstrate working of fmin()
#include
#include
using namespace std;
// Driver Code
int main()
{
double a, b;
// initializing values
a = 2.5;
b = 2.0;
// using fmin() to compute smallest of two numbers
cout << "The smallest of 2 numbers is : ";
cout << fmin(a, b);
cout << endl;
}
C++
// C++ code to demonstrate working of fmax()
#include
#include
using namespace std;
// Driver Code
int main()
{
double a, b;
// initializing values
a = 2.5;
b = 2.0;
// using fmax() to compute maximum of two numbers
cout << "The largest of 2 numbers is : ";
cout << fmax(a, b);
cout << endl;
}
C++
// C++ code to demonstrate working of fdim()
#include
#include
using namespace std;
// Driver Code
int main()
{
double a, b;
// initializing values
a = 2.5;
b = 2.0;
// using fdim() to compute positive difference of two
// numbers
cout << "The positive difference of 2 numbers is : ";
cout << fdim(a, b);
cout << endl;
}
CPP
// C++ code to demonstrate working of fma()
#include
#include
using namespace std;
// Driver Code
int main()
{
double a, b, c;
// initializing values
a = 2.5;
b = 2.0;
c = 2.5;
// using fma() to compute multiply-add
cout << "The multiply-add of 3 numbers is : ";
cout << fma(a, b, c);
}
输出
The remainder computed using fmod() is : 2.6
2. 余数() :此函数还用于返回其参数中提到的 2 个浮点数的余数(模数) 。计算的商是四舍五入的。
句法:
remainder(a,b); // a, b are 2 floating point numbers
例子:
C++
// C++ code to demonstrate working of remainder()
#include
#include
using namespace std;
// Driver Code
int main()
{
double a, b, c;
// Initializing values
a = 9.6;
b = 3.5;
// using remainder() to compute the remainder
// computes 3 as quotient (rounded)
// returns -0.9 as remainder
c = remainder(a, b);
cout << "The remainder computed using remainder() is : "
<< c;
cout << endl;
}
输出
The remainder computed using remainder() is : -0.9
3. remquo() :此函数返回余数并将余数存储在作为参数传递的变量引用中。此函数接受 3 个参数,分子、分母和必须存储商的变量的引用。
句法:
remquo(a,b,&quo); // a, b and c are three arguments
例子:
CPP
// C++ code to demonstrate working of remquo()
#include
#include
using namespace std;
// Driver Code
int main()
{
double a, b, f;
int g;
// Initializing values
a = 9.6;
b = 3.5;
// using remquo() to return quotient and remainder
// quotient stored in g
f = remquo(a, b, &g);
cout << "The remainder part of " << a << "/" << b
<< " is : " << f;
cout << endl;
cout << "The quotient part of " << a << "/" << b
<< " is : " << g;
cout << endl;
}
输出
The remainder part of 9.6/3.5 is : -0.9
The quotient part of 9.6/3.5 is : 3
4. copysign() :此函数返回一个数字,其中第一个参数的大小和第二个参数的符号。
句法:
copysign(a, b); // a and b are arguments
例子:
C++
// C++ code to demonstrate working of copysign()
#include
#include
using namespace std;
// Driver Code
int main()
{
double a, b;
// Initializing values
a = 9.6;
b = -3.5;
// using copysign()
cout << "The value returned after copysigning is : ";
cout << copysign(a, b);
cout << endl;
}
输出
The value returned after copysigning is : -9.6
5. nextafter() :此函数计算第一个参数在第二个参数方向上的下一个可表示值。
句法:
nextafter(a, b); // a and b are arguments
例子:
CPP
// C++ code to demonstrate working of nextafter()
#include
#include
using namespace std;
// Driver Code
int main()
{
double a, b;
// Initializing values
a = -3.5;
b = 0.0;
// using nextafter() to compute next approximated value
cout << "The next value approximated is : ";
cout << nextafter(a, b);
}
输出
The next value approximated is : -3.5
6. fmin() :返回两个参数中最小的一个。
句法:
fmin(a, b); // a and b are arguments
例子:
C++
// C++ code to demonstrate working of fmin()
#include
#include
using namespace std;
// Driver Code
int main()
{
double a, b;
// initializing values
a = 2.5;
b = 2.0;
// using fmin() to compute smallest of two numbers
cout << "The smallest of 2 numbers is : ";
cout << fmin(a, b);
cout << endl;
}
输出
The smallest of 2 numbers is : 2
7. fmax() :返回两个参数中的最大值。
句法:
fmax(a, b); // a and b are arguments
例子:
C++
// C++ code to demonstrate working of fmax()
#include
#include
using namespace std;
// Driver Code
int main()
{
double a, b;
// initializing values
a = 2.5;
b = 2.0;
// using fmax() to compute maximum of two numbers
cout << "The largest of 2 numbers is : ";
cout << fmax(a, b);
cout << endl;
}
输出
The largest of 2 numbers is : 2.5
8. fdim() :返回作为参数传递的数字的正差。
句法:
fdim(a, b); // a and b are arguments
例子:
C++
// C++ code to demonstrate working of fdim()
#include
#include
using namespace std;
// Driver Code
int main()
{
double a, b;
// initializing values
a = 2.5;
b = 2.0;
// using fdim() to compute positive difference of two
// numbers
cout << "The positive difference of 2 numbers is : ";
cout << fdim(a, b);
cout << endl;
}
输出
The positive difference of 2 numbers is : 0.5
9. fma() :该函数接受3 个参数并在计算后返回乘加“ x*y+z ”值。
句法:
fma(a, b, c); // a, b and c are arguments
例子:
CPP
// C++ code to demonstrate working of fma()
#include
#include
using namespace std;
// Driver Code
int main()
{
double a, b, c;
// initializing values
a = 2.5;
b = 2.0;
c = 2.5;
// using fma() to compute multiply-add
cout << "The multiply-add of 3 numbers is : ";
cout << fma(a, b, c);
}
输出
The multiply-add of 3 numbers is : 7.5