Calculate from the command line with Spigot

Slashdot it! Delicious Share on Facebook Tweet! Digg!
Anna Baburkina, 123RF

Anna Baburkina, 123RF

Lightning Calculations

The Spigot calculator distinguishes itself with its high accuracy and precise results from the shell.

While the bc command is extremely well known, there's little awareness of its competitor Spigot. Nevertheless, all it takes is a quick read of the manual and a few sample sums to start using this simple tool. The program borrows its name from the eponymous algorithm for calculating mathematical constants. However, there are other programs with the same name, which can be rather confusing. The software was developed by Simon Tatham [1].

The source for Spigot is available from [2], but you are better off simply installing it with your trusty apt software manager:

sudo apt install spigot

To test the installation, enter spigot 1 + 2 in the terminal. If you see the correct answer, the installation has worked. There's an online manual for Spigot [3] that contains further information on getting started.

General Information

One of Spigot's specialities is that it can display a virtually infinite number of decimal places. Try entering spigot pi in the terminal, and you'll see that the number of digits will scroll past indefinitely until you press Ctrl+C.

To distinguish negative input values from options and commands, place your calculations inside quotation marks and brackets (Figure 1). You can do away with these for positive numbers. At the very least, however, you should always place tasks inside commas in order to avoid confusing the shell. When using variables, you need to remove brackets because of the shell.

Figure 1: The way you input positive and negative numbers is slightly different.

For day-to-day use, you'll need to limit the number of decimal places and round values off. You can set the degree of accuracy with -d<digits> . This truncates numbers, but does not round them up or down. The rounding is determined by another set of parameters. Table 1 summarizes the main Spigot options. See Figure 2 for examples.

Table 1

Important Parameters

Parameter Notes
-d<number> Limit the number of digits, no rounding
-rn Round to nearest number
-ri Round away from zero
-rz Rounds towards zero (default)
-ru Round up towards positive infinity
-rd Round down towards negative infinity
% or mod Remainder (modulo)
^ ,   Power
'let <variable>=<value>' Define a variable
-printf Format numbers
-b <base> Convert to <base>
Figure 2: Rounding off numbers is one of Spigot's standard operations.

To display the output in a specific format, put the appropriate printf statement at the end of an input. You can find out more about the various operators with man printf . Spigot's mathematical functions, such as square root (sqrt ), are the same as those used by common programming languages. Spigot follows the order of operations in which multiplications and divisions are executed first, then additions and subtractions, and so on.

Just like bc , spigot cannot read directly from a pipe. Instead, you tell the program which input channel to read a value and the base that the number will be in. Put statements together as follows:

spigot base<base>fd:<file descriptor>

For example, the command

echo 11 | spigot base7fd:0

will spit out 8 , because fd:0 is standard input (i.e., in this case, a pipe), and 11 in base 7 is 8 in decimal.

To read the value from a file instead, use the following statement:

spigot base<base>file:<filenameI>

For example, the instruction

spigot base2file:number.txt

will read the number contained in the file number.txt and assume it is in base 2. It will spit out the number converted to decimal.

When used in a script, enclose variables in double quotes whenever they appear in a statement.

Working with Variables

Listing 1 shows how to include variables in a Spigot statement. Negative numbers are used here. The individual options include round (- rn ), cap at 50 decimal places (-d50 ), and an example of formatted output (-printf for 8 non-decimal digits and 2 decimal digits for a floating point number are also in Listing 1).

Listing 1

variables.sh

#!/bin/bash
echo "Calculating with Spigot and Variables"
read -p "First number: " a
read -p "Second number: " b
echo "Results without formatting"
echo "----------------------------"
spigot --rn -d50 \("$a"*"$b"\)
spigot --rn -d50 \("$a"/"$b"\)
echo "Results with formatting"
echo "----------------------------"
spigot --rn -d50 \("$a"*"$b"\) --printf %8.2f
spigot --rn -d50 \("$a"/"$b"\) --printf %8.2f

Figure 3 lists the commands in a script. You must either call it in the bash script or assign permissions to make it executable.

Figure 3: A small shell script demonstrates some features of Spigot, including the use of shell variables as part of a statement.

Buy this article as PDF

Express-Checkout as PDF

Pages: 4

Price $0.99
(incl. VAT)

Buy Ubuntu User

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content