"""
    APL System Variables

    UNDER DEVELOPMENT

    This is an initial version:  there is much still to be done
"""

from apl_exception import APL_Exception as apl_exception

# ------------------------------

class   apl_system_variable(object):
    """
    a simple pair: (value, confirm function)
    """
    def __init__(self,value,confirm):
        self.value = value
        self.confirm = confirm

# ------------------------------

def     confirm_bool (value):
    value = confirm_int (value)

    if value == 0:  return 0
    if value == 1:  return 1

    raise (apl_exception("DOMAIN ERROR"))

# ------------------------------

def     confirm_int (value):
    integer = int(value)

    if integer == value:    return integer

    raise (apl_exception("DOMAIN ERROR"))

# ------------------------------

system_variables = {
    "IO":       apl_system_variable(1, confirm_bool),
}

# ------------------------------

def     system_variable (name,value=None):
    """
    return the value of a system variable

    if a value is passed in, it is assigned to the system variable and the value is returned

    throws UNKNOWN SYSTEM VARIABLE if the name is not recognised
    """
    try:
        sys_var = system_variables[name.upper()]
    except KeyError:
        raise (apl_exception("UNKNOWN SYSTEM VARIABLE", name))

    if not value is None:
        sys_var.value = sys_var.confirm(value)

    return sys_var.value

# EOF

"""
    APL System Variables

    UNDER DEVELOPMENT

    This is an initial version:  there is much still to be done
"""

import  math
import  operator

from apl_exception import APL_Exception as apl_exception

# ------------------------------

class   apl_system_variable(object):
    """
    a simple pair: (value, confirm function)
    """
    def __init__(self,value,confirm):
        self.value = value
        self.confirm = confirm

# ------------------------------

def     equalCT (A,B):
    return operator.le(math.fabs(A-B),comparisonTolerance.value * max(math.fabs(A), math.fabs(B)))

# ------------------------------

def     integerCT (B):
    if not type(B) is int:
        integer = int(B+0.5)

        if equalCT(integer,B):
            return integer

    return B

# ------------------------------

def     confirm_bool (B):
    B = integerCT(B)

    if B == 0:  return 0
    if B == 1:  return 1

    raise (apl_exception("DOMAIN ERROR"))

# ------------------------------

def     confirm_int (B):
    B = integerCT(B)

    if not type(B) is int:
        raise (apl_exception("DOMAIN ERROR"))

    return B

# ------------------------------

def     confirm_real (B):
    return float(B)

# ------------------------------

indexOrigin = apl_system_variable(1, confirm_bool)

comparisonTolerance = apl_system_variable(1e-13, confirm_real)

system_variables = {
    "IO":       indexOrigin,
    "CT":       comparisonTolerance,
}

# ------------------------------

def     system_variable (name,value=None):
    """
    return the value of a system variable

    if a value is passed in, it is assigned to the system variable and the value is returned

    throws UNKNOWN SYSTEM VARIABLE if the name is not recognised
    """
    try:
        sys_var = system_variables[name.upper()]
    except KeyError:
        raise (apl_exception("UNKNOWN SYSTEM VARIABLE", name))

    if not value is None:
        sys_var.value = sys_var.confirm(value)

    return sys_var.value

# EOF

Add ⎕CT to system variables - the basis for comparison tolerance