complex number: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: complex number
| Old | New | Line | |
|---|---|---|---|
| 1 | + | Complex numbers is a mathematical concept providing solutions to equations such as $`x^2 = -1`$. In [[FPC]]'s default [[RTL|runtime library]] the unit `uComplex` defines a type `complex` and lots of operator and other functions. `u` in `uComplex` stands for the Greek letter μ, meaning “micro”, as the implementation is kept as simple as possible. | |
| 2 | + | ||
| 3 | + | In [[Extended Pascal]], which FPC plans to implement one day, the data type `complex` is defined as part of the language. | |
| 4 | + | ||
| 5 | + | ``` pascal | |
| 6 | + | program ComplexDemo; | |
| 7 | + | uses | |
| 8 | + | uComplex; | |
| 9 | + | var | |
| 10 | + | x, y: complex; | |
| 11 | + | begin | |
| 12 | + | // specifying real and imaginary part | |
| 13 | + | x := -5 + 2 * i; | |
| 14 | + | ||
| 15 | + | // specifying magnitude and phase angle | |
| 16 | + | // y := sqrt(2) * (cos(pi/4) + i * sin(pi/4)) | |
| 17 | + | y.re := 1; | |
| 18 | + | y.im := 1; | |
| 19 | + | ||
| 20 | + | x := x + y; | |
| 21 | + | // there is no toString functionality: | |
| 22 | + | writeLn('x = ', x.re, ' + ', x.im, 'i'); | |
| 23 | + | end. | |
| 24 | + | ``` | |
| 25 | + | ||
| 26 | + | Another demo, which shows how to use functions `cinit` (assign value), `cstr` (convert value to string), `csin` (sinus): | |
| 27 | + | ||
| 28 | + | ``` pascal | |
| 29 | + | program ComplexDemo2; | |
| 30 | + | uses | |
| 31 | + | uComplex; | |
| 32 | + | const | |
| 33 | + | L = 0; | |
| 34 | + | D = 3; | |
| 35 | + | var | |
| 36 | + | c1, c2: complex; | |
| 37 | + | begin | |
| 38 | + | c1 := cinit(1.0, 1.0); | |
| 39 | + | c2 := cinit(2.0, -2.0); | |
| 40 | + | WriteLn('c1 = ' + cstr(c1, L, D)); | |
| 41 | + | WriteLn('c2 = ' + cstr(c2, L, D)); | |
| 42 | + | WriteLn('c1+c2 = ' + cstr(c1+c2, L, D)); | |
| 43 | + | WriteLn('c1-c2 = ' + cstr(c1-c2, L, D)); | |
| 44 | + | WriteLn('c1*c2 = ' + cstr(c1*c2, L, D)); | |
| 45 | + | WriteLn('c1/c2 = ' + cstr(c1/c2, L, D)); | |
| 46 | + | WriteLn('sin(c1) = ' + cstr(csin(c1), L, D)); | |
| 47 | + | ReadLn; | |
| 48 | + | end. | |
| 49 | + | ``` | |
| 50 | + | ||
| 51 | + | ## see also | |
| 52 | + | ||
| 53 | + | - [[DMath]], a mathematical library also containing a complex number implementation. | |
| 54 | + | - [[LMath]], further development of LMath library, with completer support for complex numbers, where operators over them are defined. | |
| 55 | + | - [[NumLib Documentation|NumLib documentation]], where `typ.complex` is an [[Object|object]]. | |
| 56 | + | ||
| 57 | + | ## external references | |
| 58 | + | ||
| 59 | + | - [“complex number” on Wikipedia, the free online encyclopedia](https://en.wikipedia.org/wiki/Complex_number) | |
| 60 | + | - [`packages/rtl-extra/src/inc/ucomplex.pp` source code](https://gitlab.com/freepascal.org/fpc/source/-/blob/main/packages/rtl-extra/src/inc/ucomplex.pp) | |
| 61 | + | - [`packages/numlib/src/typ.pas` source code](https://gitlab.com/freepascal.org/fpc/source/-/blob/main/packages/numlib/src/typ.pas) | |
| 62 | + | ||
| 63 | + | --- | |
| 64 | + | *From the Free Pascal wiki, page "complex number", as it stood on 2022-08-06 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
