good first issue
Repository metrics
- Stars
- (0 stars)
- PR merge metrics
- (PR metrics pending)
Description
- 形如
2*pi()+3*pi(),需要一个工具函数,把 SymbolicExpr 对象拆分为 有理系数、无理系数(根号)、大数(超过 double 范围的那种)、乱七八糟的东西(如3^e()这种不可直视之物,仍然为 SymbolicExpr 对象) - 判断两个 SymbolicExpr 是否相等(哈希即可,但需要好用的哈希)
附一个勉强能用的测试脚本
import os
import math
from math import sqrt
import random
# 0.2% error allowed
ALLOW_ERROR = 0.0002
def pi():
return math.pi
def e():
return math.e
# Generate random expression (use ** first)
def randexpr(layer):
if layer <= 0:
# Return a single term only
return random.choice([str(random.randint(1,100)),"sqrt({})".format(random.randint(1,100)),"pi()","e()"])
else:
rl = random.choice(["{}","({})"]).format(randexpr(layer-1))
rr = random.choice(["{}","({})"]).format(randexpr(layer-1))
return rl+random.choice(["+","-","*","/","**"])+rr
def to_lamina(expr):
return expr.replace("**","^")
while True:
for i in range(3,7):
exp = randexpr(i)
try:
res = eval(exp)
except OverflowError as err:
print("At level",i,": OVF ",str(err))
continue
except ZeroDivisionError as err:
print("At level",i,": ZDE ",str(err))
continue
lma = to_lamina(exp)
f = open("test.lm", "w")
f.write('print("@",decimal({}));'.format(lma))
f.close()
os.system("lamina test.lm 1>out.txt 2>err.log")
fr = open("out.txt", "r")
for i in fr.readlines():
it = i.strip()
if len(it) < 1:
continue
if it[0] == "@":
its = it[1:].strip()
itv = float(its)
err = abs(itv-res)
err = (err)/abs(res)
if err > ALLOW_ERROR:
print("Error in test!!!")
print("Expression:",lma)
print("Expected:",res)
print("Read:",itv)
exit(1)
else:
print("OK,err=",err*100,"%")
break
else:
print("ERROR - RESULT NOT DETECTED")
exit(2)