Complex arithmetic
It's expected that complex numbers are 16-bit fractions. Numbers are added so will get larger than 1, though stay less than 2.
The use of variables seems necessary. They are to be avoided because they're slow.
*.
16-bit fraction multiply, discarding multiplicand
r and i
Storage for real and imaginary parts of first complex number. r is also used to store the product ac.
Variables are relatively cheap, needing only 3 words of RAM. They avoid clashes on the stack, return stack and register a.
*c
Complex multiply is complex. It can be done in 3 multiplies:
- Given a+ib and c+id
- Product is ac-bd + i(ad+bc)
- Imaginary part can be computed as (a+b)(c+d)-ac-bd
- Since multiply is slow, this is good. But sums can exceed 1
To do this
- Save r and i
- Compute product of sums and save on return stack
- Compute ac and store in r
- Compute -bd and store in a
- Complete imagingary and real parts
+c
Add 2 complex numbers. Return and a can be used to manipulate numbers.
-c
Negating a fraction can be done without adding 1. That would only affect the low-order bit.