Floating-point and advanced math

Floating-point and advanced math#

As mentioned, Bash only supports integer math. Trying to use non–integer numbers results in an error message like this:

bc is commonly used for math involving large or decimal numbers. It can be run in an interactive shell by running it with no parameters and without standard input. In the shell you can type expressions to get answers:

--quiet avoids printing a few lines of extra information when starting the shell.

The first and last lines after the command are input, and the second is the output. As we can see there are two differences from Bash arithmetic: The power operator is ^ rather than ** and the values do not wrap at 2^63^ – 1. In fact, bc can handle both huge and tiny numbers. The operators and limits are documented in info bc.


This page is a preview of The newline Guide to Bash Scripting

No discussions yet