# 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:

$ echo "$((2 * 1.1))"

bash: 2 * 1.1: syntax error: invalid arithmetic operator (error token is ".1")

`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:

$ bc --quiet

2 ^ 100

`1267650600228229401496703205376`

`quit`

`--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*