- Perl 6 Deep Dive
- Andrew Shitov
- 100字
- 2021-07-03 00:05:54
Complex numbers
In Perl 6, there is the Complex built-in type to present complex numbers.
Complex numbers have two parts, real and imaginary, and use the following syntax:
my $x = 3+4i;
my $y = -5i;
It is not necessary to explicitly spell out the real part, but the output always contains it:
say $x; # 3+4i
say $y; # -0-5i
say $z; # 0+1i
An alternative way to create a Complex number is to call a constructor (we will talk about constructors in Chapter 8, Object-Oriented Programming), as follows:
my $n = Complex.new(4, 5);
say $n; # 4+5i