3 Replies to “Easy calculator tip”

  1. Python actually makes a remarkably good calculator. Fire it up in interactive mode, and it evaluates any expression you type at the prompt. Since standard arithmetic expressions are valid python expressions, you’re away; python has bigints, so there’s no overflow to worry about, and complex numbers right in the language (spelled “1+2j”). One thing it lacks is an infinite-precision floating-point type in the base language (it uses normal 64-bit floats), but there is a ‘decimal’ module which does what you want. Oh, and you have to import the math module to get the standard maths functions (sin and cos and all that – although you can do powers, including roots, directly, with the ** operator). Plus, this being a programming language, you can assign to and use variables, define functions, work with arrays and sets, etc. Much better than bc, IMNERHO.

    Top tip: in interactive mode, the character _ means ‘the value of the last expression evaluated’, which is really handy for doing calculatorish things, for instance keeping a running total.

    — tom

  2. bc actually has functions and arrays, though per the manual page for the GNU/FSF version I have in front of me on this Ubuntu box, the POSIX definitions for the latter are quite possibly borked.

    You’ve also forced me to discover that said GNU bc also includes the ‘last’ variable, and that ‘.’ is apparently an earlier non-POSIXified shorthand for same. (Both work in a plain bc -l session here.) I still wish it would recognize an infix-type operator and just prepend the last value automatically, but this is a lot better than nothing. :)

  3. Also, this blog really needs a ‘Warning: Are you *sure* you want to post with emoticons?’ confirmation. : |

Comments are closed.