求解 C++ 中复数的 abs() 函数?
C++ 中的 abs 函数用于求解复数的绝对值。复数的绝对值(也称为模)是复平面中复数到原点的距离。计算方法为
对于复数 a+bi
mod|a+bi| = √(a2+b2)
abs() 函数会在 C++ 中返回以上运算的结果。它在 complex 库中定义,在使用前需要包含该库。
使用 C++ 中复数的 abs() 函数的示例程序
#include <iostream>
#include <complex>
using namespace std;
int main () {
float a= 13.0 , b = 5.0;
complex<double> complexnumber (a, b);
cout << "The absolute value of " << a<<"+"<<b<<"i" << " is: ";
cout << abs(complexnumber) << endl;
return 0;
}输出
The absolute value of 13+5i is: 13.9284
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP