question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

numpy unexpectedly raises zero division error if array is long enough

See original GitHub issue

Steps to reproduce:

with np.errstate(invalid='ignore', divide='raise'):
    for i in range(10000):
        z = np.zeros(i)
        try:
            z / z
        except(FloatingPointError) as exc:
            print(exc, 'at size', i)
            break

output: divide by zero encountered in true_divide at size 8001

numpy version: 1.14.2

Seems like this bug was introduced just recently, as there is no such issue with 1.13.3 and 1.14.1.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:19 (12 by maintainers)

github_iconTop GitHub Comments

2reactions
oleksandr-pavlykcommented, Nov 2, 2021

Nowadays implementations of universal functions that use MKL VML has all been moved to mkl_umath package.

The ufunc loops for certain types are registered with NumPy’s universal functions via PyUFunc_RegisterLoopForType.

One can deregister these loops using mkl_umath.restore().

1reaction
oleksandr-pavlykcommented, Aug 19, 2019

Yes, it is still reproducible:

In [2]: z = np.zeros(8001)

In [3]: z/z
/tmp/miniconda3/bin/ipython:1: RuntimeWarning: divide by zero encountered in true_divide
  #!/tmp/miniconda3/bin/python
/tmp/miniconda3/bin/ipython:1: RuntimeWarning: invalid value encountered in true_divide
  #!/tmp/miniconda3/bin/python
Out[3]: array([nan, nan, nan, ..., nan, nan, nan])

It is coming from use of MKL VML function vdDiv used to perform division.

One can turn errors off by using mkl.set_vml_mode:

import mkl     # import mkl-service
saved = mkl.vml_set_mode(
    "ha", # accuracy control
     "off", # denormalized numbers handling
     "ignore" # error mode control
)

See https://software.intel.com/en-us/mkl-developer-reference-c-vmlsetmode for more details

You can restore the VML behavior with

mkl.vml_set_mode(*saved)
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to return 0 with divide by zero - Stack Overflow
In this case, it does the divide calculation anywhere 'where' b does not equal zero. When b does equal zero, then it remains...
Read more >
NumPy 1.20.0 Release Notes
In the future, NumPy will raise an IndexError when an integer array index contains out of bound values even if a non-indexed dimension...
Read more >
the absolute basics for beginners — NumPy v1.25.dev0 Manual
The NumPy library contains multidimensional array and matrix data structures (you'll find more information about this in later sections). It provides ndarray, a ......
Read more >
NumPy quickstart — NumPy v1.24 Manual
A frequent error consists in calling array with multiple arguments, ... If an array is too large to be printed, NumPy automatically skips...
Read more >
Chapter 4. NumPy Basics: Arrays and Vectorized Computation
This chapter will introduce you to the basics of using NumPy arrays, and should be sufficient for following along with the rest of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found