Skip to content

acos(x) raises a ZeroDivisionError for small imaginary values of x #1035

Description

@DietBru

The following code snippets illustrate how to raise a ZeroDivisionError when calculating acos(1e-50j) in version '1.4.0b4':

The value 1e-20j works:

Python 3.14.3 (main, Feb  4 2026, 15:33:49) [GCC 15.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
... import mpmath
... 
>>> mpmath.__version__
'1.4.0b4'
>>> x0 = 1e-20j
>>> np.acos(x0)
np.complex128(1.5707963267948966-1e-20j)
>>> mpmath.acos(x0)  # works
mpc(real='1.5707963267948966', imag='-9.9999999999999995e-21')

The value 1e-50j raises a ZeroDivisionError:

>>> x1 = 1e-50j
>>> np.acos(x1)
np.complex128(1.5707963267948966-1e-50j)
>>> mpmath.acos(x1)
Traceback (most recent call last):
  File "<python-input-7>", line 1, in <module>
    mpmath.acos(x1)
    ~~~~~~~~~~~^^^^
  File ".../venv/lib/python3.14/site-packages/mpmath/ctx_mp_python.py", line 1195, in f
    return ctx.make_mpc(mpc_f(x._mpc_, prec, rounding))
                        ~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../lib/python3.14/site-packages/mpmath/libmp/libmpc.py", line 774, in mpc_acos
    return acos_asin(z, prec, rnd, 0)
  File ".../python3.14/site-packages/mpmath/libmp/libmpc.py", line 728, in acos_asin
    im = mpf_log1p(mpf_add(Am1, mpf_sqrt(im, wp), wp), wp)
  File ".../venv/lib/python3.14/site-packages/mpmath/libmp/libelefun.py", line 752, in mpf_log1p
    mpf_div(x, mpf_sub(u, fone, wp),
    ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^
            wp), prec, rnd)
            ^^^
  File ".../venv/lib/python3.14/site-packages/mpmath/libmp/libmpf.py", line 844, in mpf_div
    raise ZeroDivisionError
ZeroDivisionError

Cheers, dietrich

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behavior

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions