Skip to content

inaccuracy: Complex ^ Int #3246

@bsxfan

Description

@bsxfan

@aswart found another complex function that causes problems for complex-step differentiation (see also #2889).

When raising a complex number with negative real part and small imaginary part to an integer power (an everyday occurrence with complex step diferentiation), the imaginary part is wrong.

Example:

This gives an accurate result :

julia> imag(complex(-2,1e-20)|x->x*x)
-4.0e-20

But, when using ^(z::Complex, n::Integer) = z^complex(n)

julia> imag(complex(-2,1e-20)|x->x^2)
-9.797174393178826e-16 

then we get something entirely different. (MATLAB and Python give -4.0e-20).

One way to fix this would be to simply defer this case to Base.power_by_squaring(A,p), but the more general case ^(Complex,Complex) would then probably still need some attention.

julia> imag(complex(-2,1e-20)|x->Base.power_by_squaring(x,2))
-4.0e-20

CC: @jiahao

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions