Skip to content
vmathmachine edited this page Jul 14, 2022 · 2 revisions

inv is a function which, what else, computes the inverse.

If the number is real, it just takes the reciprocal of the real part. If the number is imaginary, it takes the reciprocal of the imaginary part, negates it, and returns that as an imaginary number. If the input is 0, it returns ∞, and if the input is infinite, it returns 0.

For all other cases, it computes the inverse by dividing the complex conjugate by the absolute square. That is,

1/(x+yi) = (x-yi)/(x²+y²).

There are some special cases, however. If the absolute square either overflows or underflows, the above formula doesn't work. There is an easy fix to this:

inv(x+yi) = inv((x+yi)/L)/L

where L is the lazy absolute value. When the absolute square underflows, L will be small, (x+yi)/L will be normal sized, and the absolute square will be normal sized. When the absolute square overflows, L will be large, (x+yi)/L will be normal sized, and the absolute square will be normal sized. It's a simple yet effective solution. In later updates, however, this might be changed to multiplying by a fixed small / large exponent of 2. This would be done to save on divisions, and also to make things more accurate.

If you observe the code, you might notice similar approaches are taken for the natural logarithm, the absolute value, non-integer powers, and, to a lesser extent, the square root.

Clone this wiki locally