Skip to content

documentation, tests, and bugs in seterr behavior for integer types #3199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from

Conversation

ivanov
Copy link
Contributor

@ivanov ivanov commented Apr 6, 2013

this is a work in progress. it started off as a documentation improvement for the fact, currently captured in #593 that np.seterr does not affect error handling behavior for arrays of integers (although it does mostly work for integer scalars).

But in writing a test for this behavior (which I was intending to submit and mark as a KnownFailure), I discovered two other bugs in the scalar handling. These examples assume that np.seterr(all='raise') has already been called.

  1. Integer scalar underflow (for all int types) is reported as overflow. Here's a bit of misinformation - should be UNDER-flow, not overflow
    In [28]: np.int8(-128) - np.int8(1)
    ---------------------------------------------------------------------------
    FloatingPointError                        Traceback (most recent call last)
    <ipython-input-28-2b810c89b4d5> in <module>()
    ----> 1 np.int8(-128) - np.int8(1)

    FloatingPointError: overflow encountered in byte_scalars
  1. int64 overflow is ignored during multiplication
    In [14]: d = np.int8; i = np.iinfo(d); d(i.max) * d(2)
    ---------------------------------------------------------------------------
    FloatingPointError                        Traceback (most recent call last)
    <ipython-input-14-3822c5509024> in <module>()
    ----> 1 d = np.int8; i = np.iinfo(d); d(i.max) * d(2)

    FloatingPointError: overflow encountered in byte_scalars

Good, we also get errors for int16,  int32, but not int64
In [17]: d = np.int64; i = np.iinfo(d); d(i.max) * d(2)
Out[17]: -2

@ivanov
Copy link
Contributor Author

ivanov commented Apr 6, 2013

Here's the output of running nosetests test_numeric:TestFloatExceptions -v

Test basic arithmetic function errors ... ERROR
Seterr should apply to arrays of integer types, see gh-593 ... FAIL
Seterr should apply to integer types ... FAIL

======================================================================
ERROR: Test basic arithmetic function errors
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/pi/.local/lib/python2.7/site-packages/numpy/testing/decorators.py", line 216, in knownfailer
    raise KnownFailureTest(msg)
KnownFailureTest: See ticket 1755

======================================================================
FAIL: Seterr should apply to arrays of integer types, see gh-593
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/pi/code/numpy/numpy/core/tests/test_numeric.py", line 382, in test_seterr_for_arrays_of_ints
    assert_(len(raised) == 0, pretty_repr)
  File "/home/pi/.local/lib/python2.7/site-packages/numpy/testing/utils.py", line 37, in assert_
    raise AssertionError(msg)
AssertionError: 36 errors:
  Type <type 'numpy.ndarray'>int8 did not raise fpe error 'underflow'.
  Type <type 'numpy.ndarray'>int8 did not raise fpe error 'overflow'.
  Type <type 'numpy.ndarray'>int8 did not raise fpe error 'overflow'.
  Type <type 'numpy.ndarray'>uint8 did not raise fpe error 'underflow'.
  Type <type 'numpy.ndarray'>uint8 did not raise fpe error 'overflow'.
  Type <type 'numpy.ndarray'>uint8 did not raise fpe error 'overflow'.
  Type <type 'numpy.ndarray'>int16 did not raise fpe error 'underflow'.
  Type <type 'numpy.ndarray'>int16 did not raise fpe error 'overflow'.
  Type <type 'numpy.ndarray'>int16 did not raise fpe error 'overflow'.
  Type <type 'numpy.ndarray'>uint16 did not raise fpe error 'underflow'.
  Type <type 'numpy.ndarray'>uint16 did not raise fpe error 'overflow'.
  Type <type 'numpy.ndarray'>uint16 did not raise fpe error 'overflow'.
  Type <type 'numpy.ndarray'>int32 did not raise fpe error 'underflow'.
  Type <type 'numpy.ndarray'>int32 did not raise fpe error 'overflow'.
  Type <type 'numpy.ndarray'>int32 did not raise fpe error 'overflow'.
  Type <type 'numpy.ndarray'>uint32 did not raise fpe error 'underflow'.
  Type <type 'numpy.ndarray'>uint32 did not raise fpe error 'overflow'.
  Type <type 'numpy.ndarray'>uint32 did not raise fpe error 'overflow'.
  Type <type 'numpy.ndarray'>int64 did not raise fpe error 'underflow'.
  Type <type 'numpy.ndarray'>int64 did not raise fpe error 'overflow'.
  Type <type 'numpy.ndarray'>int64 did not raise fpe error 'overflow'.
  Type <type 'numpy.ndarray'>uint64 did not raise fpe error 'underflow'.
  Type <type 'numpy.ndarray'>uint64 did not raise fpe error 'overflow'.
  Type <type 'numpy.ndarray'>uint64 did not raise fpe error 'overflow'.
  Type <type 'numpy.ndarray'>int64 did not raise fpe error 'underflow'.
  Type <type 'numpy.ndarray'>int64 did not raise fpe error 'overflow'.
  Type <type 'numpy.ndarray'>int64 did not raise fpe error 'overflow'.
  Type <type 'numpy.ndarray'>uint64 did not raise fpe error 'underflow'.
  Type <type 'numpy.ndarray'>uint64 did not raise fpe error 'overflow'.
  Type <type 'numpy.ndarray'>uint64 did not raise fpe error 'overflow'.
  Type <type 'numpy.ndarray'>int64 did not raise fpe error 'underflow'.
  Type <type 'numpy.ndarray'>int64 did not raise fpe error 'overflow'.
  Type <type 'numpy.ndarray'>int64 did not raise fpe error 'overflow'.
  Type <type 'numpy.ndarray'>uint64 did not raise fpe error 'underflow'.
  Type <type 'numpy.ndarray'>uint64 did not raise fpe error 'overflow'.
  Type <type 'numpy.ndarray'>uint64 did not raise fpe error 'overflow'.

======================================================================
FAIL: Seterr should apply to integer types
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/pi/code/numpy/numpy/core/tests/test_numeric.py", line 399, in test_seterr_for_ints
    assert_(len(raised) == 0, pretty_repr)
  File "/home/pi/.local/lib/python2.7/site-packages/numpy/testing/utils.py", line 37, in assert_
    raise AssertionError(msg)
AssertionError: 18 errors:
  Type <type 'numpy.int8'> raised wrong fpe error 'overflow encountered in byte_scalars', not 'underflow'.
  Type <type 'numpy.uint8'> raised wrong fpe error 'overflow encountered in ubyte_scalars', not 'underflow'.
  Type <type 'numpy.int16'> raised wrong fpe error 'overflow encountered in short_scalars', not 'underflow'.
  Type <type 'numpy.uint16'> raised wrong fpe error 'overflow encountered in ushort_scalars', not 'underflow'.
  Type <type 'numpy.int32'> raised wrong fpe error 'overflow encountered in int_scalars', not 'underflow'.
  Type <type 'numpy.uint32'> raised wrong fpe error 'overflow encountered in uint_scalars', not 'underflow'.
  Type <type 'numpy.int64'> raised wrong fpe error 'overflow encountered in long_scalars', not 'underflow'.
  Type <type 'numpy.int64'> did not raise fpe error 'overflow'.
  Type <type 'numpy.uint64'> raised wrong fpe error 'overflow encountered in ulong_scalars', not 'underflow'.
  Type <type 'numpy.uint64'> did not raise fpe error 'overflow'.
  Type <type 'numpy.int64'> raised wrong fpe error 'overflow encountered in longlong_scalars', not 'underflow'.
  Type <type 'numpy.int64'> did not raise fpe error 'overflow'.
  Type <type 'numpy.uint64'> raised wrong fpe error 'overflow encountered in ulonglong_scalars', not 'underflow'.
  Type <type 'numpy.uint64'> did not raise fpe error 'overflow'.
  Type <type 'numpy.int64'> raised wrong fpe error 'overflow encountered in long_scalars', not 'underflow'.
  Type <type 'numpy.int64'> did not raise fpe error 'overflow'.
  Type <type 'numpy.uint64'> raised wrong fpe error 'overflow encountered in ulong_scalars', not 'underflow'.
  Type <type 'numpy.uint64'> did not raise fpe error 'overflow'.

----------------------------------------------------------------------
Ran 3 tests in 0.007s

FAILED (errors=1, failures=2)

@njsmith
Copy link
Member

njsmith commented Apr 6, 2013

My gut feeling is that the overflow versus underflow thing isn't worth fixing; instead we should create a new error type "wraparound" that means, integer overflow/underflow. (This has other advantages too -- integer and floating overflow are very different, and the code for manipulating the floating point error flags is stupidly expensive, so it'd be better if the integer operations didn't do it.)

I'm pretty sure there's code that's supposed to detect int64 multiply wraparound -- guess it's buggy!

@charris
Copy link
Member

charris commented Mar 28, 2014

@ivanov Are you going to pursue this? Otherwise I'm going to close it.

@ivanov
Copy link
Contributor Author

ivanov commented Mar 28, 2014

Wow, blast from the past - had no idea this was still open. I'll see what I can do

@njsmith
Copy link
Member

njsmith commented Mar 28, 2014

It would really make more sense to add a new error type for integer
wraparound...
On 28 Mar 2014 22:27, "Paul Ivanov" notifications@github.com wrote:

Wow, blast from the past - had no idea this was still open. I'll see what
I can do


Reply to this email directly or view it on GitHubhttps://github.com//pull/3199#issuecomment-38969956
.

@charris
Copy link
Member

charris commented Jan 18, 2015

@ivanov I'm closing this. Let me know if you still want to work on it.

@charris charris closed this Jan 18, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants