Advanced Math Calculation in Bash using GNU bc

  • HOME
  • >
  • BASH
  • >
  • Advanced Math Calculation in Bash using GNU bc
Last Updated: 
Tags:  bash bc math

Our previous post Performing Math Calculation in Bash was an introduction to basic operations (addition, substraction, division, multiplication) of elementary arithmetic in a bash shell or by using bc. In this new Advanced Math in Bash post, we cover how to do advanced arithmetic (aka algebra) using mathematical functions like sine, cosine, tangent and so on. We will also cover how to write your own mathematical functions using GNU bc command line tool.

About GNU bc

bc stand for basic calculator, it was preceded by dc, a cross-platform reverse-polish desk calculator one of the oldest Unix utilities.

bc, for basic calculator, is "an arbitrary precision calculator language" with syntax similar to the C programming language. bc is typically used as either a mathematical scripting language or as an interactive mathematical shell.

Math Library Functions

Using the -l option of the GNU bc command line utility will load the Math library and set the default value of scale to 20. The predefined functions that comes with the math library are:

s (x)
 The sine of x, x is in radians.

c (x)
 The cosine of x, x is in radians.

a (x)
 The arctangent of x, arctangent returns radians.

l (x)
 The natural logarithm of x.

e (x)
 The exponential function of raising e to the value x.

j (n,x)
 The bessel function of integer order n of x.

[root@host ~]$ bc -l <<< "l(3)"
1.09861228866810969139

Special Expressions

GNU bc provide few special expressions that make it even more powerful.

length ( expression )
The value of the length function is the number of significant digits in the expression.

read ( )
The read function (an extension) will read a number from the standard input, regardless of where the function occurs. Beware, this can cause problems with the mixing of data and program in the standard input. The best use for this function is in a previously written program that needs input from the user, but never allows program code to be input from the user. The value of the read function is the number read from the standard input using the current value of the variable ibase for the conversion base.

scale ( expression )
The value of the scale function is the number of digits after the decimal point in the expression.

sqrt ( expression )
The value of the sqrt function is the square root of the expression. If the expression is negative, a run time error is generated.

[root@host ~]$ bc -l <<< "sqrt(5)"
2.23606797749978969640

Going further: Write a function

GNU bc allows you to define your own Functions and makes the language very powerful.

define name ( parameters ) { newline
    auto_list   statement_list }

There is a wide list of open source projects with a lot of functions available. You should check all the X-BC resources.

  • extensions.bc: contains functions of trigonometry, exponential functions, functions of number theory and some mathematical constants.
  • scientific_constants.bc: contains particle masses, basic constants, such as speed of light in the vacuum and the gravitational constant.

Another amazing resource is the GNU bc FAQ on phodd.net. You will find a large amount of functions in the file funcs.bc that can be of good help and gives you some good examples (include round-up, ceil, floor, etc).

To go further, I highly recommend reading the GNU bc Manual.

Related bash posts that you may like
What is the Bash Null Command?
Learn about the Bash null command, also known as the POSIX shell colon command. This post cover concrete use cases and pitfalls to avoid.
How To Format Date and Time in Linux, macOS, and Bash?
Find out how to manipulate date and time on linux and macOS systems as well as natively in the Bash shell. This post covers all you need to know to format a date from your shell.
How To Use Option as Meta Key in macOS Terminal?
The Meta Key is a modifier key that can be quite helpful to improve your productivity while working in a terminal and bash. This post cover how to enable from the command line the Meta Key in macOS Terminal.