星期三, 九月 12, 2012

Terminal calculators

A terminal and Emacs fan can use local calculators.  For example, one can put the below codes in ~/bin/jisuan as a bash script:

#!/usr/bin/env bash
if [ $# -eq 2 ]
then
    echo "scale=$1; $2"|bc
else
    echo "scale=4; $1"|bc
fi
This script calculate the second argument at the command line with the precision of the first if there are exactly two arguments.  If only one or more than three arguments are fed, the default precision is 4, with the first argument as the expression.

You can try jisuan 6 1/998001; jisuan 9 1/998001; ... jisuan 10000 1/998001 in a terminal, you will get
0.000001
0.000001002
0.000001002003
...
Oh, yes, this decimal recurs on [000 001 002 ... 999].

Another calculator in Emacs can be invoked by Alt-x calc, which is also very convenient.

没有评论: