complex
The file builtins\complex.e (not an autoinclude) contains a basic implementation of complex number arithmetic.
A complex number contains a real and imaginary part, which simplify some calculations. For a proper explanation see https://en.wikipedia.org/wiki/Complex_number.
These routines are fully supported by pwa/p2js.
A complex number contains a real and imaginary part, which simplify some calculations. For a proper explanation see https://en.wikipedia.org/wiki/Complex_number.
These routines are fully supported by pwa/p2js.
Example:
include complex.e
--constant complex i = complex_new(0,1) -- NB: 'i' is not provided as standard
--constant complex i = {0,1} -- (equivalent, if you prefer)
complex a = complex_new(5,3),
b = complex_new(4,-3),
x = complex_add(a,b),
y = complex_mul(a,b),
z = complex_div(y,b)
string sa = complex_sprint(a),
sb = complex_sprint(b),
sx = complex_sprint(x),
sy = complex_sprint(y),
sz = complex_sprint(z)
printf(1,"(%s) + (%s) = %s\n",{sa,sb,sx}) -- (5+3i) + (4-3i) = 9
printf(1,"(%s) * (%s) = %s\n",{sa,sb,sy}) -- (5+3i) * (4-3i) = 29-3i
printf(1,"(%s) / (%s) = %s\n",{sy,sb,sz}) -- (29-3i) / (4-3i) = 5+3i
| bool res = |
|
|
|
|
| atom res = |
|
| atom res = |
|
| atom res = |
|
| atom res = |
|
| complex res = |
|
| complex res = |
|
| complex res = |
|
| complex res = |
|
| complex res = |
|
| complex res = |
|
| complex res = |
|
| atom res = |
|
| atom res = |
|
| atom res = |
|
| complex res = |
|
| complex res = |
|
| complex res = |
|
| complex res = |
|
| complex res = |
|
| complex res = |
|
| complex res = |
|
| complex res = |
|
| complex res = |
|
| complex res = |
|
| complex res = |
|
| complex res = |
|
| string res = |