From 794a6c4511ced84c74bc8f2dd8cd8f277925a6ac Mon Sep 17 00:00:00 2001 From: Pauli Virtanen Date: Thu, 10 Dec 2009 19:12:41 +0000 Subject: ENH: emit ComplexWarning when casting complex to real (addresses #1319) Casting complex numbers to real discards the imaginary part, which may be unexpected. For safety, emit a warning when this occurs. --- numpy/core/numeric.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'numpy/core/numeric.py') diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index e35fca07b..8c4fa2980 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -19,7 +19,8 @@ __all__ = ['newaxis', 'ndarray', 'flatiter', 'ufunc', 'seterrcall', 'geterrcall', 'errstate', 'flatnonzero', 'Inf', 'inf', 'infty', 'Infinity', 'nan', 'NaN', 'False_', 'True_', 'bitwise_not', - 'CLIP', 'RAISE', 'WRAP', 'MAXDIMS', 'BUFSIZE', 'ALLOW_THREADS'] + 'CLIP', 'RAISE', 'WRAP', 'MAXDIMS', 'BUFSIZE', 'ALLOW_THREADS', + 'ComplexWarning'] import sys import warnings @@ -32,6 +33,16 @@ from numerictypes import * if sys.version_info[0] < 3: __all__.extend(['getbuffer', 'newbuffer']) +class ComplexWarning(RuntimeWarning): + """ + Warning that is raised when casting complex numbers to real. + + Casting a complex number to real discards its imaginary part, and + this behavior may not be what is intended in all cases. + + """ + pass + bitwise_not = invert CLIP = multiarray.CLIP -- cgit v1.2.1