errstate(**kwargs) Context manager for floating-point error handling. Using an instance of `errstate` as a context manager allows statements in that context to execute with a known error handling behavior. Upon entering the context the error handling is set with `seterr` and `
| 385 | |
| 386 | @set_module('numpy') |
| 387 | class errstate: |
| 388 | """ |
| 389 | errstate(**kwargs) |
| 390 | |
| 391 | Context manager for floating-point error handling. |
| 392 | |
| 393 | Using an instance of `errstate` as a context manager allows statements in |
| 394 | that context to execute with a known error handling behavior. Upon entering |
| 395 | the context the error handling is set with `seterr` and `seterrcall`, and |
| 396 | upon exiting it is reset to what it was before. |
| 397 | |
| 398 | .. versionchanged:: 1.17.0 |
| 399 | `errstate` is also usable as a function decorator, saving |
| 400 | a level of indentation if an entire function is wrapped. |
| 401 | |
| 402 | .. versionchanged:: 2.0 |
| 403 | `errstate` is now fully thread and asyncio safe, but may not be |
| 404 | entered more than once. |
| 405 | It is not safe to decorate async functions using ``errstate``. |
| 406 | |
| 407 | Parameters |
| 408 | ---------- |
| 409 | kwargs : {divide, over, under, invalid} |
| 410 | Keyword arguments. The valid keywords are the possible floating-point |
| 411 | exceptions. Each keyword should have a string value that defines the |
| 412 | treatment for the particular error. Possible values are |
| 413 | {'ignore', 'warn', 'raise', 'call', 'print', 'log'}. |
| 414 | |
| 415 | See Also |
| 416 | -------- |
| 417 | seterr, geterr, seterrcall, geterrcall |
| 418 | |
| 419 | Notes |
| 420 | ----- |
| 421 | For complete documentation of the types of floating-point exceptions and |
| 422 | treatment options, see `seterr`. |
| 423 | |
| 424 | **Concurrency note:** see :ref:`fp_error_handling` |
| 425 | |
| 426 | Examples |
| 427 | -------- |
| 428 | >>> import numpy as np |
| 429 | >>> olderr = np.seterr(all='ignore') # Set error handling to known state. |
| 430 | |
| 431 | >>> np.arange(3) / 0. |
| 432 | array([nan, inf, inf]) |
| 433 | >>> with np.errstate(divide='ignore'): |
| 434 | ... np.arange(3) / 0. |
| 435 | array([nan, inf, inf]) |
| 436 | |
| 437 | >>> np.sqrt(-1) |
| 438 | np.float64(nan) |
| 439 | >>> with np.errstate(invalid='raise'): |
| 440 | ... np.sqrt(-1) |
| 441 | Traceback (most recent call last): |
| 442 | File "<stdin>", line 2, in <module> |
| 443 | FloatingPointError: invalid value encountered in sqrt |
| 444 |
no outgoing calls
searching dependent graphs…