This is a read-only archive. Find the latest Linux articles, documentation, and answers at the new Linux.com!

Linux.com

Feature: Tools & Utilities

Fast, flexible, calculating from the command line

By Ben Morgan on March 11, 2008 (9:00:00 AM)

Share    Print    Comments   

GUI calculators may be user-friendly, but they don't offer much comfort for a command-line power user. Fortunately, console-based tools such as bc, Genius, and Calc offer distinct advantages over their GUI counterparts.

Command-line calculators offer most if not all of the features GUI calculators have, and have a lot more going for them:

  • They have interactive and non-interactive usage modes.
  • They can be used in a script, as well as run their own scripts.
  • They are efficient to use -- your hands remain on the keyboard.
  • They have many built-in functions and variables.
  • They support on-the-fly definition of new functions and variables.

While Genius is the most advanced (and probably stable) of the aforementioned console calculators, Calc is the most user-friendly, and provides more capabilities than the average user will need. You can find Calc in the repositories of many distributions, and it can also run under Windows.

Calc does not have a man page; instead, all of its detailed help is located in /usr/share/calc/help/, and can be viewed using any plain text viewer, or from within Calc. The command calc help opens an overview of the help in less; you can then choose the topic that interests you and run calc help topic. To view all the help, use calc help full. Calc provides more general resources and examples in /usr/share/calc.

Calc uses intuitive mathematical symbols and functions, such as + - * / ^ ** % or sqrt(x), sin(x), tan(x), which makes it easy to learn. In addition, because it is a C-style calculator, people familiar with C-style programming will find that the syntax of Calc will comes naturally (although there are differences; see calc help unexpected).

To start, type the command calc. Calc's prompt is a semicolon. The following Calc session will give you a taste of the capabilities of the tool:

C-style arbitrary precision calculator (version 2.12.2.2) Calc is open software. For license details type: help copyright [Type "exit" to exit, or "help" for help.] ; ## 5 to the power of 2 is ; 5 ^ 2 25 ; 5 ** 2 25 ; ; ## For a comment, you need 2 ## ; /* But these comments work too */ ; ; ## Floating-point divide ; 111 / 2 55.5 ; ## Integer divide ; 111 // 2 55 ; ; ## How to define a function ; ## The short way: ; define f(x) = x*2 f(x) defined ; ; ## The long way: ; define g(first, second) { ;; first -= second; ;; first ** 2; ;; first // 1.5; ;; first++; ;; print "done"; ;; return first; ;; } g(first,second) defined ; ; ## The functions in action ; ## The space is optional ; g( f(2), 3 ) done 2 ; ; g( f(5), 1 ) done 10 ;

With no arguments, Calc starts in an interactive shell. You can also start it with an expression to evaluate, such as calc 2+3. After evaluation it will exit, unless you also supply the -i argument, in which case it will drop to the interactive shell.

You can place collections of variables and functions into a resource file, often suffixed with .cal, which the program can read and use. The syntax for resource files is identical to that of the interactive shell. As an example, suppose you find yourself in chemistry class using elemental atomic masses often. You could make a resource file with variables, functions, and commands, such as the following, named periodic.cal:

#!/usr/bin/calc -i read H = 1.00794 He = 4.0026 Li = 6.941 Be = 9.012182 B = 10.811 C = 12.0107 N = 14.0067 O = 15.9994 ## ... and so forth print "You are using the periodic resource for calc."

You can then load a resource file with a command like calc -i read periodic.cal. Alternatively, you can make it executable with chmod +x periodic.cal and then run it directly. Once you load the resource file, you have access to all the variables that you defined in it:

You are using the periodic resource for calc. ; ## And now I am in the interactive shell ; O 15.9994 ; C+O*2 44.0095 ;

Most people use Linux with X running, instead of living solely on the command line. A convenient way to use Calc in such an environment is to start it within a virtual terminal, such as aterm: aterm -e calc or aterm -e calc -i read /pathtoyourresource/periodic.cal. Be forewarned, however, that the latest version of Calc suffers from a bug that causes it to quit after an attempt to call a nonexistent function. Therefore, when you are exploring functions, run Calc within a terminal that will not exit when Calc does, so that you do not visually lose what you already typed.

If you're a command-line fan, you don't need to start a GUI to run a calculator. Programs like Calc put plenty of power at your fingertips without your ever having to move a mouse.

Ben Morgan is a student in Senegal. He has been using Linux since 2002.

Share    Print    Comments   

Comments

on Fast, flexible, calculating from the command line

Note: Comments are owned by the poster. We are not responsible for their content.

Fast, flexible, calculating from the command line

Posted by: Anonymous [ip: 87.127.117.246] on March 11, 2008 09:56 AM
if you are used to python syntax then this work quite well

alias pc='python -ic "from __future__ import division; from math import *"'

(put it in your .bashrc, start a new terminal, and type pc. CTRL+D to exit)

#

Re: Fast, flexible, calculating from the command line

Posted by: Anonymous [ip: 134.252.17.121] on March 12, 2008 01:23 AM
ipython is pretty sweet for this sort of thing too.

#

Fast, flexible, calculating from the command line

Posted by: Anonymous [ip: 80.178.114.234] on March 11, 2008 10:59 AM
and if you're into reverse-polish-notation: <a href="http://pessimization.com/software/orpie/">orpie</a> or emacs -nw --eval "(calc)"

#

Fast, flexible, calculating from the command line

Posted by: Anonymous [ip: 87.65.11.78] on March 11, 2008 01:26 PM
<a href="http://qalculate.sourceforge.net/features.html">Qalculate</a> is really great.

#

Re: Fast, flexible, calculating from the command line

Posted by: Anonymous [ip: 80.200.229.146] on March 12, 2008 10:35 AM
Yes it's really great, i use it mostly for unit convertion.

#

Fast, flexible, calculating from the command line

Posted by: Anonymous [ip: 76.10.183.178] on March 11, 2008 01:40 PM
bc ftw! Installed on pretty much any Linux system I've used. The same cannot be said for calc.

#

Fast, flexible, calculating from the command line

Posted by: Anonymous [ip: 64.24.177.124] on March 11, 2008 02:20 PM
Mathomatic is a a fast, easy command line calculator too. http://mathomatic.org

#

Fast, flexible, calculating from the command line

Posted by: Anonymous [ip: 24.31.187.238] on March 11, 2008 05:19 PM
I just use R for this type of stuff.

#

Fast, flexible, calculating from the command line

Posted by: Anonymous [ip: 83.38.5.91] on March 11, 2008 05:27 PM
dc (reverse-polish-notation) the best!

#

Fast, flexible, calculating from the command line

Posted by: Anonymous [ip: 128.32.198.160] on March 11, 2008 06:17 PM
Do any of these programs support calculations with units, and conversions between different unit systens?

#

Fast, flexible, calculating from the command line

Posted by: Anonymous [ip: 24.31.187.238] on March 11, 2008 07:19 PM
"Do any of these programs support calculations with units, and conversions between different unit systens?"



There's a package called units.

#

Fast, flexible, calculating from the command line

Posted by: Anonymous [ip: 77.40.205.243] on March 11, 2008 10:13 PM
Is there a page with cal-files, like the "periodic.cal" in the article, for calc?

#

Fast, flexible, calculating from the command line

Posted by: Vaida Bogdan on March 12, 2008 12:18 PM
A script I use from the net: calcme "30*27.3"

#!/usr/bin/perl

# Usage calcme <string_to_calculate> [<output_format>]
# Input is a string like (10+3)/7 or "(10 + 3) / 7"
# Output is the calculated result of the string
# Optional formatting can supplied as 2nd parameter.

if (@ARGV == 0 || @ARGV > 2)
{
die("Usage: $0 <\"formula_to_calculate\"> [<output_format>]\n");
}

$format = "";
$calcme = $ARGV[0];
(@ARGV == 2) && ($format = $ARGV[1]);

$output = eval($calcme);

if(@ARGV == 1)
{
print(STDOUT "$output\n");
}
else
{
printf(STDOUT "$format\n", $output);
}
exit(0);

#

Fast, flexible, calculating from the command line

Posted by: Anonymous [ip: 70.5.253.216] on March 13, 2008 03:19 AM
Reverse Polish lovers, check out these two:

- rplsh (http://www.astro.umd.edu/~rauch/rplsh/)
- rpl/2 (http://www.systella.fr/~bertrand/rpl2/english.html)

For more, see http://www.finseth.com/rpnrpl.html, not all in the unix vein.

#

Fast, flexible, calculating from the command line

Posted by: Anonymous [ip: 81.241.251.49] on March 13, 2008 01:18 PM
My ultimate calc is irb ( interactive ruby) with auto-completion
* fully scriptable (it's ruby)
* simple syntax for math
* auto-completion
* readline support good for history navigation

# irb$ irb
irb(main):001:0> include Math
=> Object
irb(main):002:0> 2 ** 5
=> 32
irb(main):003:0> 111 /10
=> 11
irb(main):004:0> 111 /10.0
=> 11.1
irb(main):005:0> sin(0.5)
=> 0.479425538604203
irb(main):007:0> def f(x) sin(x) * cos(x) end
=> nil
irb(main):008:0> f 0.6
=> 0.466019542983613
irb(main):009:0> f(0.6)
=> 0.466019542983613

#

Fast, flexible, calculating from the command line

Posted by: Anonymous [ip: 213.100.162.179] on March 15, 2008 02:41 PM
I keep this in my .profile

calc ()
{
awk "BEGIN{ print $* }" </dev/null
}

#

Fast, flexible, calculating from the command line

Posted by: Anonymous [ip: 99.240.152.84] on March 20, 2008 07:03 PM
and what happened to 'sc' the command-line spreadsheet?

Last time I tried it ( last year? ) I couldn't get it to compile ( someone's maintained it in the last 6 years? )

~/test/downloads/sc-7.16 # make
gcc -DSYSV3 -O2 -pipe -c -o xmalloc.o xmalloc.c
xmalloc.c:9: error: conflicting types for ‘malloc’
make: *** [xmalloc.o] Error 1

Anyways, if someone fixes it to run in modern *nix systems, it is super-sweet compared with mere-arithmetic!

ibiblio.org/pub/Linux/apps/financial/spreadsheet/

PS How come I can't post here from within Konqueror? this is LINUX.com, isn't it?

#

This story has been archived. Comments can no longer be posted.



 
Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya