Skip to content

Commit

Permalink
Merge pull request #34 from flotwig/allow-abrupt-decimals
Browse files Browse the repository at this point in the history
Support decimals beginning with only a dot
  • Loading branch information
camilonova authored Jun 18, 2018
2 parents de41e02 + a7ec301 commit e5c2312
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
4 changes: 3 additions & 1 deletion py_expression_eval/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,9 @@ def isNumber(self):
while self.pos < len(self.expression):
code = self.expression[self.pos]
if (code >= '0' and code <= '9') or code == '.':
str += self.expression[self.pos]
if (len(str) == 0 and code == '.' ):
str = '0'
str += code
self.pos += 1
self.tokennumber = float(str)
r = True
Expand Down
12 changes: 12 additions & 0 deletions py_expression_eval/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,18 @@ def count(increment):
self.assertEqual(parser.evaluate("count(inc)", variables={"inc": 5}), 5)
self.assertEqual(parser.evaluate("count(inc)", variables={"inc": 5}), 10)

def test_decimals(self):
parser = Parser()

self.assertEqual(parser.parse(".1").evaluate({}), parser.parse("0.1").evaluate({}))
self.assertEqual(parser.parse(".1*.2").evaluate({}), parser.parse("0.1*0.2").evaluate({}))
self.assertEqual(parser.parse(".5^3").evaluate({}), float(0.125))
self.assertEqual(parser.parse("16^.5").evaluate({}), 4)
self.assertEqual(parser.parse("8300*.8").evaluate({}), 6640)

with self.assertRaises(ValueError):
parser.parse("..5").evaluate({})


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

0 comments on commit e5c2312

Please sign in to comment.