Lightning Calculations
The Spigot calculator distinguishes itself with its high accuracy and precise results from the shell.
|
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.
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.
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> |
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.
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.
Pages: 4
In Linux, the need for text processing programs is a big one: System configuration, system management, and data exchange all use text-based files. With Awk, you have a powerful tool at your fingertips for text editing and targeted modification.
From simple queries to complex menus: Using dialog, you can create a graphical interface for shell scripts with only a few extra lines of code.
The Shell Script Compiler converts scripts into binaries, which protects against accidental changes but also carries some pitfalls.
Gentoo-based Calculate Linux announces version 11.0, which is designed to help users meet their business needs, and migrate to and deploy a GNU/Linux-based solution.
Does the ingenious script your wrote last month look like confusing spaghetti code today? We show some proven style guidelines that can provide order and readability.
© 2025 Linux New Media USA, LLC – Legal Notice