# [Tutor] precision for complex numbers

ThreeBlindQuarks threesomequarks at proton.me
Thu Apr 6 12:32:04 EDT 2023

```Alan,

Complex numbers are, well, complex, but really are implemented mostly as two linked floating point numbers with the second one considered the imaginary component called i in mathematics. There are other such constructs like quaternions with an i,j,k set of orthogonal parts and even octonions with 7 of these. So your point is that the limits of floats will apply to any such composite data type as unlike the way  Python has made unlimited integers, they don't expand and float as needed.

This has been a very common theme as people keep asking in just about any programming language for more digits or to maintain ALL digits and perhaps especially now that many fairly complex, ahem, calculations are routinely done in which losing some terminal bits can add up.

So could this be done? I believe not easily and perhaps not at all.

One choice might be to change the current uses of "double" and pretty much break all existing code. As an example, compilers and interpreters that expect things placed on the stack of some fixed size would now need to look for a more elaborate structure that begins with some notation of how big the payload is and perhaps point to it as stored elsewhere. Connections to libraries of code currently written in C or FORTRAN or RUST might now need to be called by first shrinking your number back into the old format and losing data and converting any answers back.

And it would be SLOW.

An alternative would be to create new data types such as infinifloat and they might only be usable for a small set of operations that can handle them and perhaps eventually lots would. You would, of course, have to have a type like infinicomplex to match.

As hardware changes and evolves, we may well migrate to some new standards with something like 512 bits assigned to each and every numeric type but by then, someone will always want ever more precision.

I think a very serious problem to consider is that most numbers in the real world simply cannot be represented in binary. Something as simple as 1/3 expands infinitely and if your machine tries to make a form of floating point that contains it all, it will use all available memory and die. The same applies to most square roots or irrational numbers like π or things like a repeated multiplication like the product of a huge number of probabilities that in some sense just would keep getting longer and longer.

The person asking here, of course, was not asking for infinite precision, just more precision. This is a very common need albeit I suspect people needing this may end up using languages designed to be way more efficient than python.

But there is no current solution WITHIN python, albeit there likely are modules you can import and use with care as not everything will accept ...

Sent with Proton Mail secure email.

------- Original Message -------
On Thursday, April 6th, 2023 at 5:09 AM, Alan Gauld via Tutor <tutor at python.org> wrote:

> On 05/04/2023 18:30, Msd De wrote:
>
> > Is there a way to increase precision for complex numbers for python?
>
>
> The precison is the same as for floats and as such is fixed by
> the IEEE float standard.
>
> How much more precision did you have in mind, and would you also
> need that extra precision for floats too, since presumably you'd
> need to interact with non-complex numbers at times too?
>
> There may be more precise complex implemetatioms around as
> third-party modules but they will likely be very much slower
> in operation. But I've not heard of any such modules.
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
```