Commit 08389229 authored by adam j hartz's avatar adam j hartz

add support for 'hybrid' number mode

parent 71cc0d9e
......@@ -168,7 +168,7 @@ Fraction.__repr__ = Fraction.__str__ = _frac_repr
def tako_num(x):
nmode = builtins.__tako_env__['TAKO_SETTINGS'].number_mode
if nmode == 'python':
if nmode in {'python', 'hybrid'}:
return ast.literal_eval(x)
elif nmode == 'exact':
try:
......@@ -181,21 +181,37 @@ def tako_num(x):
else:
raise TakoError('Invalid number mode: %s' % nmode)
def make_tako_op(opdict):
def _tako_op(x, y):
nmode = builtins.__tako_env__['TAKO_SETTINGS'].number_mode
if nmode in opdict:
return opdict[nmode](x, y)
else:
raise TakoError('Invalid number mode: %s' % nmode)
return _tako_op
def tako_div(x, y):
nmode = builtins.__tako_env__['TAKO_SETTINGS'].number_mode
if nmode == 'python':
def _exact_div(x, y):
if any(isinstance(i, (float, complex)) for i in (x, y)):
return x / y
elif nmode == 'exact':
if any(isinstance(i, (float, complex)) for i in (x, y)):
return x / y
else:
n = Fraction(x, y)
if n.denominator == 1:
return n.numerator
return n
else:
raise TakoError('Invalid number mode: %s' % nmode)
n = Fraction(x, y)
if n.denominator == 1:
return n.numerator
return n
def _exact_floordiv(x, y):
if any(isinstance(i, (float, complex)) for i in (x, y)):
return x // y
else:
return Fraction(x, y).__floor__()
tako_div = make_tako_op({'python': lambda x,y: x / y,
'exact': _exact_div,
'hybrid': _exact_div})
tako_floordiv = make_tako_op({'python': lambda x,y: x // y,
'exact': _exact_floordiv,
'hybrid': _exact_floordiv})
def tako_floordiv(x, y):
......
......@@ -103,7 +103,7 @@ defaults = {
'IGNOREEOF': False,
'INDENT': ' ',
'MULTILINE_PROMPT': '.',
'NUMBER_MODE': 'exact',
'NUMBER_MODE': 'hybrid',
'PRETTY_PRINT_RESULTS': True,
'PROMPT': DEFAULT_PROMPT,
'PUSHD_MINUS': False,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment