Using Complex Numbers

The C++ standard library can handle complex numbers. Complex numbers may be used by including . The class std::complex is defined as a template, allowing complex numbers to use different value types, such as float and double.

You can construe complex numbers using the real and imaginary components; you can also construe them using the polar() function. For example:

std::complex cf(1); // 1 + 0istd::complex cd(2, 3); // 2 + 3istd::complex cld =    std::polar(4, 5); // 4 e^(5i)

Obtain the magnitude, argument, and square of the magnitude using the abs(), arg(), and norm() member functions. For example:

std::cout << cd.abs() << ' '          << cd.arg() << ' '          << cd.norm() << std::endl;

Read complex numbers from std::cin and write them to std::cout. For example:

std::cin >> cd;std::cout << cd << std::endl;

Complex numbers may be in the following three formats for std::cin:

  • 1 (meaning 1 + 0i)
  • (2) (meaning 2 + 0i)
  • (3,4) (meaning 3 + 4i)

Ignore any whitespace during input. Complex numbers are always output in the third format by std::cout.

Arithmetic operators such as +, -, *=, and /= work as expected for complex numbers. So do transcendental functions such as sin, cosh, exp, log, pow, and sqrt.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS