像整数一样,C++ 11引入了一些基本的内置函数,用于处理日常编程以及竞争性编程所需的浮点数的简单数学计算。本文讨论了一些引入的功能。
1. fmod() :此函数用于返回其参数中提到的2个浮点数的余数(模数) 。计算出的商将被截断。
2. restder() :此函数还用于返回其参数中提到的2个浮点数的剩余(模数)。计算出的商是四舍五入的。
3. remquo() :此函数返回余数,并且还将余数存储在作为参数传递的变量引用中。此函数采用3个参数,分子,分母和变量的引用(必须存储商)。
// C++ code to demonstrate working of
// fmod(), remainder() and remquo()
#include
#include
using namespace std;
int main() {
double a, b, c, d, f;
int g;
// initializing values
a = 9.6;
b = 3.5;
// using fmod() to compute the remainder
// computes 2 as quotient (truncated)
// returns 2.6 as remainder
d = fmod(a,b);
// using remainder() to compute the remainder
// computes 3 as quotient (rounded)
// returns -0.9 as remainder
c = remainder(a,b);
// using remquo() to return quotient and remainder
// quotient stored in g
f = remquo(a,b,&g);
cout << "The remainder computed using fmod() is : " <
输出:
The remainder computed using fmod() is : 2.6
The remainder computed using remainder() is : -0.9
The remainder part of 9.6/3.5 is : -0.9
The quotient part of 9.6/3.5 is : 3
4. copysign() :此函数返回一个数量级为1st的数字和2nd符号的符号的数字。
5. nextafter() :此函数沿第二个参数的方向计算第一个参数的下一个可表示值。
// C++ code to demonstrate working of
// nextafter() and copysign()
#include
#include
using namespace std;
int main()
{
double a, b, c;
// initializing values
a = 9.6;
b = -3.5;
c = 0.0;
// using copysign()
cout << "The value returned after copysigning is : ";
cout << copysign(a,b);
cout << endl;
// using nextafter() to compute next approximated value
cout << "The next value approximated is : ";
cout << nextafter(c,b);
}
输出:
The value returned after copysigning is : -9.6
The next value approximated is : -4.94066e-324
6. fmin() :返回两个参数中最小的一个。
7. fmax() :返回两个参数中的最大值。
8. fdim() :返回作为参数传递的数字的正差。
9. fma() :此函数接受3个参数,并在计算后返回乘加“ x * y + z ”值。
// C++ code to demonstrate working of
// fmin(), fmax(), fdim(), fma()
#include
#include
using namespace std;
int main()
{
double a, b, c;
// initializing values
a = 2.5;
b = 2.0;
c = 2.5;
// using fmax() to compute maximum of two numbers
cout << "The largest of 2 numbers is : ";
cout << fmax(a,b);
cout << endl;
// using fmin() to compute smallest of two numbers
cout << "The smallest of 2 numbers is : ";
cout << fmin(a,b);
cout << endl;
// using fdim() to compute positive difference of two numbers
cout << "The positive difference of 2 numbers is : ";
cout << fdim(a,b);
cout << endl;
// using fma() to compute multiply-add
cout << "The multiply-add of 3 numbers is : ";
cout << fma(a,b,c);
}
输出:
The largest of 2 numbers is : 2.5
The smallest of 2 numbers is : 2
The positive difference of 2 numbers is : 0.5
The multiply-add of 3 numbers is : 7.5
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。