« | Main | »

June 08, 2009

I was reducing the size of some cereal box images and wanted to find the quotient 210 / 275, and immediately (and stupidly) tried expr, and of course it didn't work, because it was using integers. Not wanting to use a browser I made fexpr, which is a floating point version of expr; I'm sure it exists somewhere (in many places), but oh well. It's a little recursive decent parser and, I think, conforms to most of expr. There's a test suite of about 60 tests you can run by ./fexpr -test, you can see the help with ./fexpr -help, and you can turn on tracing for the parsing-curious by ./fexpr -trace .... A couple examples:
$./fexpr 1 + 3 "*" 4 / 7
2.71428571428571
$ ./fexpr -trace 1
1.0
1.0
$ ./fexpr -trace 1 + 2
+
. 1.0
. 2.0
3.0
$ ./fexpr -trace 1 + 2 "*" 3
+
. 1.0
. *
. . 2.0
. . 3.0
7.0
$ ./fexpr -trace 3 "*" 1 + 2
+
. *
. . 3.0
. . 1.0
. 2.0
5.0
$ ./fexpr -trace 3 / 2
/
. 3.0
. 2.0
1.5
$ ./fexpr -trace 2 / 4
/
. 2.0
. 4.0
0.5


Posted by jeff at June 8, 2009 10:03 AM