Skip to content

Commit

Permalink
Merge pull request #28 from uncountableinc/brendan/scientific-notation-2
Browse files Browse the repository at this point in the history
Support negative expoonents
  • Loading branch information
logansnow17 authored May 1, 2023
2 parents dd534fd + 78dc261 commit 03fbdf8
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion hotxlfp/grammarparser/lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@


def t_SCIENTIFIC_NOTATION_E(t):
r'(?<=\d)\s*e\s*(?=\d)' # Lookahead to not confuse with variables and functions
r'(?<=\d)\s*e\s*(?=\-?\s*?\.?\d)' # Lookahead to not confuse with variables and functions
return t


Expand Down
3 changes: 3 additions & 0 deletions hotxlfp/grammarparser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,16 @@ def p_expression_number(self, p):
expression : expression_decimal_number
| expression_decimal_number PERCENT
| expression_decimal_number SCIENTIFIC_NOTATION_E expression_decimal_number
| expression_decimal_number SCIENTIFIC_NOTATION_E MINUS expression_decimal_number
"""
if len(p) == 2: # expression_decimal_number
p[0] = p[1]
elif len(p) == 3: # expression_decimal_number PERCENT
p[0] = lambda args, p1=p[1]: to_number(p1, args) * 0.01
if len(p) == 4: # expression_decimal_number SCIENTIFIC_NOTATION_E expression_decimal_number
p[0] = lambda args, p1=p[1], p3=p[3]: p1(args) * (10 ** p3(args))
if len(p) == 5: # expression_decimal_number SCIENTIFIC_NOTATION_E MINUS expression_decimal_number
p[0] = lambda args, p1=p[1], p4=p[4]: p1(args) * (10 ** -p4(args))

def p_expression_string(self, p):
"""
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name="hotxlfp",
version="0.0.11+unc.24",
version="0.0.11+unc.25",
packages=[
"hotxlfp",
"hotxlfp._compat",
Expand Down
7 changes: 5 additions & 2 deletions tests/test_formula_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ def test_tensors(self):
_test_equation(equation="MAX(MAX(2, a1 * 2), 100)", variables={"a1": [5, 4]}, answer=[100, 100])
_test_equation(equation="5", variables={"a1": [5, 4]}, answer=[5])
_test_equation(equation="SQRT(100)", variables={"a1": [5]}, answer=[10])

def test_scientific_notation(self):
_test_equation(equation="2e2", variables={"a1" : [1.1]}, answer=[200])
_test_equation(equation="5(m)", variables={"m" : [10]}, answer=[50])
Expand All @@ -391,7 +391,10 @@ def test_scientific_notation(self):
_test_equation(equation="(2)e(4)", variables={"a1" : [1.1]}, should_fail=True)
_test_equation(equation="0.2e2", variables={"a1" : [1.1]}, answer=20)
_test_equation(equation="0.2e0.2", variables={"a1" : [1.1]}, answer=0.2 * (10 ** 0.2))

_test_equation(equation="2e-1", variables={"a1" : [1.1]}, answer=0.2)
_test_equation(equation="-2e1", variables={"a1" : [1.1]}, answer=-20)
_test_equation(equation="-2e-1", variables={"a1" : [1.1]}, answer=-0.2)
_test_equation(equation="-2e-.1", variables={"a1" : [1.1]}, answer=-2 * (10 ** (-.1)))

if __name__ == "__main__":
unittest.main()

0 comments on commit 03fbdf8

Please sign in to comment.