Floating Level Arithmetic within the Bourne Once more Shell (BASH)

This can be a temporary submit on fast methods to carry out floating level arithmetic within the GNU Bourne Once more Shell (BASH), both on the command immediate or in a shell script. It’s partially a followup to the submit Ten Nice Fast Calculators for Laptop Customers and overlaps a few of the content material on this earlier submit.

The Bourne Once more Shell, or bash, is the default command line processor (shell) for Mac OS X, the cygwin atmosphere on MS Home windows, and plenty of different Unix and Unix-like methods. Particularly when you work with numbers, chances are you’ll need to carry out fast floating level arithmetic on the command immediate (shell) or in shell scripts. This submit covers 5 methods to carry out floating level arithmetic in bash: the bc arbitrary precision calculator language, the awk sample scanning and processing language, the perl programming language, the python programming language, and the ruby programming language.

Integer Solely Arithmetic in BASH

Notice: One can carry out integer arithmetic and integer arithmetic alone (no floating level) through the use of the Unix expr utility and the bash $[N op M] syntax the place N and M are any integers and op is any arithmetic operator (+, -, *, /). For instance:


[email protected] ~
$ expr 1 + 2
3

[email protected] ~
$ echo $[1 + 2]
3

These integer-only arithmetic strategies had been described extra absolutely within the earlier submit Ten Nice Fast Calculators for Laptop Customers.

5 Methods to Carry out Floating Level Arithmetic in BASH

(1) BC

The next instructions present tips on how to carry out floating level arithmetic (addition, subtraction, multiplication, and division) on the bash shell immediate utilizing the bc arbitrary precision calculator language. Notice the necessity to escape the multiply operator * with a backslash or enclose the arithmetic expression in single quotes. Notice the usage of the backtick myvar=`command` syntax to assign the outcomes of the arithmetic to a bash variable.

Notice that there needs to be no house between the variable title and the equal signal within the task, in any other case an error happens.


[email protected] ~
$ echo 1.1 + 2.2 | bc -l
3.3

[email protected] ~
$ echo 1.1 - 2.2 | bc -l
-1.1

[email protected] ~
$ echo 1.1 * 2.2 | bc -l
1.1  (THIS IS WRONG!!!!)

[email protected] ~
$ echo 1.1 * 2.2 | bc -l
2.42   (THIS IS RIGHT!!!)

[email protected] ~
$ echo '1.1 * 2.2' | bc -l
2.42    (THIS IS RIGHT!!!)

[email protected] ~
$ echo '1.1 / 2.2' | bc -l
.50000000000000000000

[email protected] ~
$ myvar=`echo '1.1 / 2.2' | bc -l`

[email protected] ~
$ echo $myvar
.50000000000000000000


(2) AWK

The next instructions present tips on how to carry out floating level arithmetic (addition, subtraction, multiplication, and division) on the bash immediate utilizing the awk sample scanning and processing language. The AWK operators and capabilities for elevating a quantity to an influence (**,^), the sq. root (sqrt(x)), the pure logarithm (log(x)), the sine operate (sin(x)), cosine operate (cos(x)), and the arctangent (atan2(y,x)) are additionally proven. Notice the usage of the backtick myvar=`command` syntax to assign the outcomes of the arithmetic to a bash variable. AWK has the benefit that it is rather outdated and is nearly at all times obtainable on Unix and Unix-like methods.

Notice that there needs to be no house between the variable title and the equal signal within the variable task.


[email protected] ~
$ echo - | awk '{print 1.1 + 2.2}'
3.3

[email protected] ~
$ echo - | awk '{print 1.1 - 2.2}'
-1.1

[email protected] ~
$ echo - | awk '{print 1.1 * 2.2}'
2.42

[email protected] ~
$ echo - | awk '{print 1.1 / 2.2}'
0.5

[email protected] ~
$ echo - | awk '{print 1.1 ^ 2.2}'
1.23329

[email protected] ~
$ echo - | awk '{print 1.1 ** 2.2}'
1.23329

[email protected] ~
$ echo - | awk '{print log(10.0)}'
2.30259

[email protected] ~
$ echo - | awk '{print sqrt(2.0)}'
1.41421

[email protected] ~
$ echo - | awk '{print sin(1.0)}'
0.841471

[email protected] ~
$ echo - | awk '{print cos(1.0)}'
0.540302

[email protected] ~
$ echo - | awk '{print atan2(1.0, 1.0)}'
0.785398

[email protected] ~
$ myvar=`echo - | awk '{print 1.1 + 2.2}'`

[email protected] ~
$ echo $myvar
3.3


(3) PERL

The next instructions present tips on how to carry out floating level arithmetic (addition, subtraction, multiplication, and division) utilizing the Perl programming language on the bash immediate. Notice the usage of the backtick myvar=`command` syntax to assign the outcomes of the arithmetic to a bash variable. Perl is extensively used and virtually at all times already put in on Unix and Unix-like methods.

Notice that there needs to be no house between the variable title and the equal signal within the variable task.


[email protected] ~
$ perl -e 'print 1.1 + 2.2'
3.3
[email protected] ~
$ perl -e 'print 1.1 - 2.2'
-1.1
[email protected] ~
$ perl -e 'print 1.1 * 2.2'
2.42
[email protected] ~
$ perl -e 'print 1.1 / 2.2'
0.5
[email protected] ~
$ myvar2=`perl -e 'print 1.1 + 2.2'`

[email protected] ~
$ echo $myvar2
3.3


(4) PYTHON

The next instructions present tips on how to carry out floating level arithmetic (addition, subtraction, multiplication, and division) utilizing the Python programming language on the bash immediate. Notice the usage of the backtick myvar=`command` syntax to assign the outcomes of the arithmetic to a bash variable. Python is extensively used and ceaselessly already put in on Unix and Unix-like methods.

Notice that there needs to be no house between the variable title and the equal signal within the variable task.


[email protected] ~
$ python -c 'print 1.1 + 2.2'
3.3

[email protected] ~
$ python -c 'print 1.1 - 2.2'
-1.1

[email protected] ~
$ python -c 'print 1.1 * 2.2'
2.42

[email protected] ~
$ python -c 'print 1.1 / 2.2'
0.5

[email protected] ~
$ myvar3=`python -c 'print 1.1 + 2.2'`

[email protected] ~
$ echo $myvar3
3.3

(5) RUBY

The next instructions present tips on how to carry out floating level arithmetic (addition, subtraction, multiplication, and division) utilizing the Ruby programming language on the bash immediate. Notice the usage of the backtick myvar=`command` syntax to assign the outcomes of the arithmetic to a bash variable. Ruby is extensively used and generally already put in on Unix and Unix-like methods.

Notice that there needs to be no house between the variable title and the equal signal within the variable task.


[email protected] ~
$ ruby -e 'print 1.1 + 2.2'
3.3
[email protected] ~
$ ruby -e 'print 1.1 - 2.2'
-1.1
[email protected] ~
$ ruby -e 'print 1.1 * 2.2'
2.42
[email protected] ~
$ ruby -e 'print 1.1 / 2.2'
0.5
[email protected] ~
$ myvar4=`ruby -e 'print 1.1 + 2.2'`

[email protected] ~
$ echo $myvar4
3.3


These identical instructions to carry out floating level arithmetic might be included in Bourne Once more Shell (bash) scripts.

Conclusion

There are no less than 5 methods to carry out floating level arithmetic underneath the Unix or GNU Bourne Once more Shell (bash), both on the command immediate or in shell scripts. These are the bc arbitrary precision calculator language, the awk sample scanning and processing language, the perl programming language, the python programming language, and the ruby programming language.

AWK is more than likely to be preinstalled on a Unix or Unix-like system, adopted by BC and PERL. Python is now pretty widespread though nonetheless not as widespread as PERL. RUBY is turning into pretty widespread, however it’s nonetheless not as widespread as Python within the writer’s expertise. BC has the particular benefit that it’s an arbitrary precision calculator whereas the others are often 32 or 64 bit precision floating level by default.

© 2012 John F. McGowan

Concerning the Writer

John F. McGowan, Ph.D. solves issues utilizing arithmetic and mathematical software program, together with creating video compression and speech recognition applied sciences. He has in depth expertise creating software program in C, C++, Visible Primary, Mathematica, MATLAB, and plenty of different programming languages. He’s most likely finest recognized for his AVI Overview, an Web FAQ (Steadily Requested Questions) on the Microsoft AVI (Audio Video Interleave) file format. He has labored as a contractor at NASA Ames Analysis Heart concerned within the analysis and improvement of picture and video processing algorithms and know-how. He has printed articles on the origin and evolution of life, the exploration of Mars (anticipating the invention of methane on Mars), and low-cost entry to house. He has a Ph.D. in physics from the College of Illinois at Urbana-Champaign and a B.S. in physics from the California Institute of Expertise (Caltech). He might be reached at [email protected].