Skip to content

Commit 2503f13

Browse files
Implement math functions in expression evaluator
Add support for mathematical functions: log, log10, and sqrt.
1 parent e2db89f commit 2503f13

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

cogs/counting.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
)
1414
import ast
1515
import operator
16+
import math
1617
import asyncio
1718
import time
1819
from typing import Optional
@@ -291,6 +292,15 @@ def eval_node(node):
291292
op = type(node.op)
292293
if op in operators:
293294
return operators[op](eval_node(node.operand))
295+
elif isinstance(node, ast.Call):
296+
if isinstance(node.func, ast.Name):
297+
if node.func.id == "log":
298+
args = [eval_node(a) for a in node.args]
299+
return math.log(*args)
300+
if node.func.id == "log10":
301+
return math.log10(eval_node(node.args[0]))
302+
if node.func.id == "sqrt":
303+
return math.sqrt(eval_node(node.args[0]))
294304
raise TypeError("Unsupported type")
295305

296306
try:

0 commit comments

Comments
 (0)