Complex floating point types

complex numberscomputational mathematicsfloating pointnumerical methodsreference-request

I'm implementing my own programming language for fun, and have been playing around with the implementation of various floating point numeric types. I'd like to think about implementing a numeric type to represent complex numbers.

i) I've been reading around, and my understanding is that there is no equivalent of the IEEE-754 standard (which defines standard floating-point representations of real numbers) for complex numbers. Is that correct, or have I missed something?

ii) All programming languages that I'm aware of implement complex by storing a pair of (either single- or double-precision) floating-point numbers, which represent the Cartesian form of a complex number $x + iy$. Is anyone aware of a programming language that does things differently?

I'd like to consider implementing a numeric type that represents complex numbers in polar form $re^{i\theta}$, storing $r$ as an unsigned floating-point number and $\theta$ as an unsigned fixed-point number. This would be quite a non-standard thing to do (but remember I'm just doing my programming language for fun). As far as I'm aware, there are very few settings that use unsigned floating-point numbers, but it feels like this would be an appropriate use for them. I'd want $\theta$ to be stored as fixed-point in order to give an even spread around the origin.

Is anyone aware of any work that has been done on the numerical consequences of such a choice? For example: what is the relative importance, in a typical mathematical workflow, or addition vs multiplication of complex numbers (addition being easier in Cartesian form, multiplication being easier in polar form)? Are there any references that anyone could point me to please?

NB – please note that I'm not interested in the existence of any hardware support for these numeric types. Since I'm doing my language for fun, I'm happy to do things in software as a learning exercise.

Finally – does anyone have any suggestions about how a complex equivalent of Inf should behave?

Best Answer

Regarding complex version of IEEE-754

It is true that no complex version of IEE754 exists, but the same design rules as for IEEE-754 can be applied for complex arithmetic. However, this is more subtle than it seems in the first place.

For example the direct cartesian implementation of the complex multiplication (i.e. $(a+ib)(c+id)$ does not work with $inf$. Consider for example $(\text{inf} + 0i)(\text{inf} + 0i)$. the correct result would obviously be $(\text{inf} + 0i)$. But the direct floating point evaluation will give $(\text{inf} +\text{NaN}i)$ which is not correct. So the implementation needs to take care of these corner cases to always generate a correct result.

Furthermore for special functions, it is even not always obvious what the correct behavior with respect to +0, -0, +inf, -inf and NaN should be. And for complex arithemtic you even have combinations of +0 + inf, +inf + nan in real and imaginary part and so on.

Some papers which may be useful are (Kahan is a very autoritative source on this topic)

You could of course decide to not have inf and/or NaN. But then your arithemtic is not algrebaically closed which means not all operations generate a result, but require some sort of exception handling.

Regarding the implementation

You are completely free how to implement the complex numbers, as real and imaginary pair or in polar form. Once the behavior of the complex arithmetic is defined (especially with repsect to the special float and complex float values, as discussed above) the implementation just has to give the correct results.

There are considerations such as speed, but that you know already. polar form is usually not used because addition is quite expensive and I believe there are also issues with accuracy. However, if this is not an issue and you only need to multiply and divide then polar form is the way to go I would say.