My DotNET application has a limited scripting language build in (modelled loosely on VBScript) mainly for post-processing numbers, points and vectors. I've just added support for complex numbers, but I'm struggling with the notation.
I don't want to use the A + Bi notation, since it is not a clear division if A or B are defined as equations already:
31 * 5 + 6 + -5i
This could be interpreted as:
A = 31 * 5 + 6 B = -5i
and:
A = 31 * 5 B = 6 + -5i
None of the programming languages I know have native support for complex numbers. I'm thinking something like the following might work, but I'd appreciate any input on this:
{31 * 5} + {6 + -5}i
complex(31 * 5, 6 + -5)
r{31 * 5} i{6 + -5}
If your desire is to simply differentiate the real from the imaginary component for a complex number, I'd do one of the following.
Either of those would be simple to process even with a simple parser and they're close enough to the mathematical notation so as not to confuse. I prefer the first since it seems more explicit to me and uses symbols that are unlikely to confuse.
Do your users have a specific notation they use? Could you pick one as close to theirs as possible? In my case I'd use a + bi so I'd say {31 * 5} + {6 + -5}i however, if they used to the functional form then I suggest complex(31 * 5, 6 + -5).
Since you're using .net you might want to use the DLR to give your scripting python or ruby syntax?
It seems you are using explicit multiplication in your examples (i.e. you require A * B, rather than A B ).
In that case why not simply use the i suffix directly following the value as in
Then you may need to decide about i or j, I've seen both...
This i-suffix trick is not unlike the scientific notication and is e (3.1415e7 for example)
Edit (following David's comments)
The format above can become confusing, depending on the audience, one way to clarify this may be to only allow for imaginary literals, and to include these into a complex notation derived from your existing vector notation. When imaginary numbers or complex number require an expression to designate them, the syntax would require the explicit "function-looking" syntax such as Imaginary(i) and Complex(r, i).
Parsing rules:
In short:
That's seems consistent and simple enough, but then again, only the true end-users can tell.
Most of the languages I can find that have a complex number builtin type (such as Python and Lisp) use something like: