求解 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

更新时间:2019 年 8 月 7 日

378 次浏览

开启你的 职业生涯

通过课程认证进行认证

开始学习
广告
© . All rights reserved.