summaryrefslogtreecommitdiff
path: root/numpy/numarray/util.py
blob: 01002f1949f3c8740bf146104b3b47f6dd107c2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import os
import numpy

__all__ = ['MathDomainError', 'UnderflowError', 'NumOverflowError', 
           'handleError', 'get_numarray_include_dirs']

class MathDomainError(ArithmeticError): pass
class UnderflowError(ArithmeticError): pass
class NumOverflowError(OverflowError, ArithmeticError): pass

def handleError(errorStatus, sourcemsg):
    """Take error status and use error mode to handle it."""
    modes = numpy.geterr()
    if errorStatus & numpy.FPE_INVALID:
        if modes['invalid'] == "warn":
            print "Warning: Encountered invalid numeric result(s)", sourcemsg
        if modes['invalid'] == "raise":
            raise MathDomainError(sourcemsg)
    if errorStatus & numpy.FPE_DIVIDEBYZERO:
        if modes['dividebyzero'] == "warn":
            print "Warning: Encountered divide by zero(s)", sourcemsg
        if modes['dividebyzero'] == "raise":
            raise ZeroDivisionError(sourcemsg)
    if errorStatus & numpy.FPE_OVERFLOW:
        if modes['overflow'] == "warn":
            print "Warning: Encountered overflow(s)", sourcemsg
        if modes['overflow'] == "raise":
            raise NumOverflowError(sourcemsg)
    if errorStatus & numpy.FPE_UNDERFLOW:
        if modes['underflow'] == "warn":
            print "Warning: Encountered underflow(s)", sourcemsg
        if modes['underflow'] == "raise":
            raise UnderflowError(sourcemsg)


def get_numarray_include_dirs():
    base = os.path.dirname(numpy.__file__)
    newdirs = [os.path.join(base, 'numarray')]
    return newdirs