UCALC equivalent

Scott David Daniels Scott.Daniels at Acm.Org
Fri Aug 12 15:18:11 EDT 2005


max wrote:
> Larry Bates <lbates at syscononline.com> wrote in
> news:42FCDCA7.7030107 at syscononline.com: 
>>Python has built in eval function and doesn't require a library.
> 
> Are you kidding? Read the original post a little more closely. The 
> o.p. is looking for a library that evaluates mathematical expressions 
> and is callable from python code.

He is absolutely correct.
 From the web page referenced:

     ucDefineFunction("area(length,width) = length*width");
     ucDefineFunction("frac(x)=abs(abs(x)-int(abs(x)))");
     ucDefineFunction("test() = 5");
     ucDefineFunction("abc(x, y=10) = x + y");
     ucDefineFunction("shl[x, y] = x * 2^y");

     cout.precision(16);
     cout << ucEval("frac(150/17) * area(20,30)") << endl;
     cout << ucEval("abc(5)-abc(3,4)*(#b01101 shl 1)")
          << endl;


The python equivalent:

     exec "def area(length,width): return length*width"
     exec "def frac(x): return abs(abs(x) - int(abs(x)))"
     exec "def test(): return 5"
     exec "def abc(x, y=10): return x + y"
     exec "def shl(x, y): return x * 2^y"

     print eval("frac(150/17) * area(20,30)")
     print eval("abc(5) - abc(3,4) * shl(0x0E, 1)")

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list