C++ 中的复数

本文涉及的产品
云原生数据库 PolarDB 分布式版,标准版 2核8GB
简介: 复杂库实现复杂类以包含笛卡尔形式的复数以及多个函数和重载以对其进行操作。

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第7天,点击查看活动详情


复杂库实现复杂类以包含笛卡尔形式的复数以及多个函数和重载以对其进行操作。


  • real()  – 它返回复数的实数部分。

  • imag()  – 它返回复数的虚部。
// 演示real()和imag()函数使用的程序
#include <iostream> 
// 对于std::complex、std::real、std::imag
#include <complex>  
using namespace std;
// 驱动器功能
int main()
{ 
// 定义复数:(10+2i)
std::complex<double> mycomplex(10.0, 2.0);
// 使用实函数打印实零件
cout << "Real part: " << real(mycomplex) << endl;
cout << "Imaginary part: " << imag(mycomplex) << endl;
return 0;
}
复制代码


输出:

Real part: 10
Imaginary part: 2
复制代码

  • abs()  – 它返回复数的绝对值。

  • arg()  – 它返回复数的参数。
//说明 arg() 和 abs() 用法的程序
#include <iostream> 
// 对于 std::complex, std::abs, std::atg
#include <complex>
using namespace std;
// 驱动器功能
int main ()
{ 
// 驱动程序函数定义复数:(3.0+4.0i)
std::complex<double> mycomplex (3.0, 4.0);
// 打印复数的绝对值
cout << "The absolute value of " << mycomplex << " is: ";
cout << abs(mycomplex) << endl;
// 打印复数的参数
cout << "The argument of " << mycomplex << " is: ";
cout << arg(mycomplex) << endl;
return 0;
}
复制代码


输出:

The absolute value of (3,4) is: 5
The argument of (3,4) is: 0.927295
复制代码


polar()  – 它根据幅度和相位角构造一个复数。


实数=幅度余弦(相位角)虚数=幅度正弦(相位角)

// 演示polar()用法的程序
#include <iostream> 
// std::complex, std::polar
#include <complex>
using namespace std;
// 驱动器功能
int main ()
{
cout << "The complex whose magnitude is " << 2.0;
cout << " and phase angle is " << 0.5;
// polar()的使用
cout << " is " << polar (2.0, 0.5) << endl;
return 0;
}
复制代码


输出:

The complex whose magnitude is 2 and phase angle is 0.5 is (1.75517,0.958851)
复制代码


norm()  – 它用于查找复数的范数(绝对值)。如果 z = x + iy 是实部 x 和虚部 y 的复数,则 z 的复共轭定义为 z'(z bar) = x – iy,z 的绝对值(也称为范数)定义为:

40.png



// 说明 norm() 用法的示例
#include <iostream> 
// 对于 std::complex, std::norm
#include <complex>
using namespace std;
// 驱动器功能
int main ()
{ 
// 初始化复合体:(3.0+4.0i)
std::complex<double> mycomplex (3.0, 4.0);
// norm() 的使用
cout << "The norm of " << mycomplex << " is "
  << norm(mycomplex) <<endl;
return 0;
}
复制代码


输出:


The norm of (3,4) is 25.
复制代码


conj()  – 它返回复数 x 的共轭。复数(实数,imag)的共轭是(实数,-imag)。

// 说明cong()的用法
#include <iostream>
using namespace std;
// std::complex, std::conj
#include <complex>  
// 驱动程序
int main ()
{
std::complex<double> mycomplex (10.0,2.0);
cout << "The conjugate of " << mycomplex << " is: ";
// 使用cong()
cout << conj(mycomplex) << endl;
return 0;
}
复制代码

输出:


The conjugate of (10,2) is (10,-2)
复制代码


proj()  – 它返回 z(复数)在黎曼球面上的投影。z 的投影是 z,但复无穷大除外,它们映射到复数值,实数分量为无穷大,虚分量为 0.0 或 -0.0(如果支持),具体取决于 z 虚部的符号。


// 说明proj()的用法
#include <iostream>
using namespace std;
// For std::complex, std::proj
#include <complex>
// 驱动程序
int main()
{
  std::complex<double> c1(1, 2);
  cout << "proj" << c1 << " = " << proj(c1) << endl;
  std::complex<double> c2(INFINITY, -1);
  cout << "proj" << c2 << " = " << proj(c2) << endl;
  std::complex<double> c3(0, -INFINITY);
  cout << "proj" << c3 << " = " << proj(c3) << endl;
}
复制代码

输出:


proj(1,2) = (1,2)
proj(inf,-1) = (inf,-0)
proj(0,-inf) = (inf,-0)
复制代码


sqrt()  – 使用主分支返回 x 的平方根,其切割沿负实轴。


// 说明sqrt() 的用法
#include <iostream>
using namespace std;
// For std::ccomplex, stdc::sqrt
#include <complex>
// 驱动程序
int main()
{ 
  // sqrt() 的使用
  cout << "Square root of -4 is "
    << sqrt(std::complex<double>(-4, 0)) << endl
    << "Square root of (-4,-0), the other side of the cut, is "
    << sqrt(std::complex<double>(-4, -0.0)) << endl;
}
复制代码


输出:


Square root of -4 is (0,2)
Square root of (-4,-0), the other side of the cut, is (0,-2)



相关实践学习
跟我学:如何一键安装部署 PolarDB-X
《PolarDB-X 动手实践》系列第一期,体验如何一键安装部署 PolarDB-X。
目录
相关文章
|
2天前
|
C语言
浮点数中的零
Julia 支持三种浮点类型:Half(16位),Single(32位)和 Double(64位)精度。浮点数包含正零和负零,二者相等但二进制表示不同,如`bitstring`所示:0.0为全零位,而-0.0仅最高位为1。
|
2天前
leetcode-537:复数乘法
leetcode-537:复数乘法
14 0
|
10月前
浮点数的世界 0.1 + 0.2 = 0.30000000000000004?
今天看到一个有趣的新闻,浮点数计算导致的灾难,借此机会再理解一下浮点数的原理。
72 1
|
11月前
|
存储 Java
详解浮点数
1.什么是浮点数 在计算机系统的发展过程中,曾经提出过多种方法表示实数,但是到为止使用最广泛的是浮点表示法。浮点表示法,即用浮点数来表示实数,所谓浮点数,意思是小数点的位置不是固定的,是可以浮动的。浮点数采用IEEE 754这个标准作为统一的标准。该标准中严格定义个浮点数的表示格式、转化过程。 下面简单介绍一下IEEE 754标准。 IEEE 754的核心就是用科学计数法来表示实数,然后将表示结果转为二进制的方式,方便计算机存储。
296 0
|
12月前
复数四则运算
复数四则运算
86 0
|
12月前
|
机器学习/深度学习 SQL 算法
用Python实现复数的四则运算
用Python实现复数的四则运算
|
存储
5.6.1_浮点数的表示
计算机组成原理之浮点数的表示
243 0
5.6.1_浮点数的表示