Differences between revisions 1 and 65 (spanning 64 versions)
Revision 1 as of 2007-05-02 23:08:44
Size: 1939
Editor: redondos
Comment:
Revision 65 as of 2009-11-16 19:39:04
Size: 3341
Editor: GreyCat
Comment: move the footnote back to the bottom
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
[[Anchor(faq22)]] <<Anchor(faq22)>>
Line 3: Line 3:
["BASH"] does not have built-in floating point arithmetic: [[BASH]] does not have built-in floating point [[ArithmeticExpression|arithmetic]]:
Line 10: Line 10:
For better precision, an external program must be used, e.g. {{{bc}}}, {{{awk}}} or {{{dc}}}: Bash cannot do ''anything'' with floating point numbers, ''including'' compare them to each other(*). Instead, an external program must be used, e.g. {{{bc}}}, {{{awk}}} or {{{dc}}}:
Line 19: Line 19:
If you are trying to compare floating point numbers, be aware that a simple ''x < y'' is not supported by all versions of {{{bc}}}. Alternatively, you could use this: Same example with {{{dc}}} (reversed polish calculator, lighter than bc):
{{{
    $ echo "3 k 10 3 / p" | dc
}}}

{{{k}}} sets the precision to 3, and {{{p}}} prints the value of the top of the stack with a newline. The stack is not altered, though.

If you are trying to compare floating point numbers, be aware that a simple ''x < y'' is not supported by all versions of {{{bc}}}.
Line 22: Line 29:
    # This would work with some versions, but not HP-UX 10.20.
    # The here string feature, inherited from rc->zsh->ksh93 was
    # introduced in bash 2.05b-alpha1
    imadev:~$ bc <<< '1 < 2'
    syntax error on line 1,
}}}

Alternatively, you could use this:

{{{
    # Bash
Line 27: Line 45:
This example substracts 2.5 from 1.4, and checks the sign of the result. If it is negative, the former number is less than the latter. This example subtracts 2.5 from 1.4, and checks the sign of the result. If it is negative, the first number is less than the second.
Line 29: Line 47:
{{{awk}}} can be used for calculations, too: Portable version:
Line 32: Line 50:
    $ awk 'BEGIN {printf "%.3f\n", 10 / 3}' /dev/null     # Bourne
    case "`echo "1.4 - 2.5" | bc`" in
      -*) echo "1.4 is less than 2.5";;
    esac
}}}

[[AWK]] can be used for calculations, too:

{{{
    $ awk 'BEGIN {printf "%.3f\n", 10 / 3}'
Line 36: Line 63:
There is a subtle but important difference between the {{{bc}}} and the {{{awk}}} solution here: {{{bc}}} reads commands and expressions ''from standard input''. {{{awk}}} on the other hand evaluates the expression as ''part of the program''. Expressions on standard input are ''not'' evaluated, i.e. {{{echo 10/3 | awk '{print $0}'}}} will print {{{10/3}}} instead of the evaluated result of the expression. There is a subtle but important difference between the {{{bc}}} and the {{{awk}}} solution here: {{{bc}}} reads commands and expressions from ''standard input''. {{{awk}}} on the other hand evaluates the expression as ''part of the program''. Expressions on standard input are ''not'' evaluated, i.e. {{{echo 10/3 | awk '{print $0}'}}} will print {{{10/3}}} instead of the evaluated result of the expression.
Line 38: Line 65:
This explains why the example uses {{{/dev/null}}} as an input file for {{{awk}}}: the program evaluates the {{{BEGIN}}} action, evaluating the expression and printing the result. Afterwards the work is already done: it reads its standard input, gets an end-of-file indication, and terminates. If no file had been specified, {{{awk}}} would wait for data on standard input. Newer versions of zsh and the KornShell have built-in floating point arithmetic, together with mathematical functions like {{{sin()}}} or {{{cos()}}} .
Line 40: Line 67:
Newer versions of KornShell93 have built-in floating point arithmetic, together with mathematical functions like {{{sin()}}} or {{{cos()}}} . Caveat: Many problems that look like floating point arithmetic can in fact be solved using integers only, and thus do not require these tools: Problems dealing with rational numbers. For example, to check if two numbers {{{x}}} and {{{y}}} are in a ratio of 4:3 or 16:9 you may use something along these lines:
{{{
# Variables x and y are integers
if (( $x*9-$y*16==0 )) ; then
   echo "16:9."
elif (( $x*3-$y*4==0 )) ; then
   echo "4:3."
else
   echo "Neither 16:9 nor 4:3."
fi
}}}
A more elaborate test could tell if the ratio is closest to 4:3 or 16:9 without using floating point arithmetic. Note that this very simple example that apparently involves floating point numbers and division is solved with integers and no division. If possible, it's usually more efficient to convert your problem to integer arithmetic than to use floating point arithmetic.

(*)Actually, I lied. It can print them, using {{{printf}}} and one of the {{{%e}}} or {{{%f}}} or {{{%g}}} format strings. But that's all.

How can I calculate with floating point numbers instead of just integers?

BASH does not have built-in floating point arithmetic:

    $ echo $((10/3))
    3

Bash cannot do anything with floating point numbers, including compare them to each other(*). Instead, an external program must be used, e.g. bc, awk or dc:

    $ echo "scale=3; 10/3" | bc
    3.333

The "scale=3" command notifies bc that three digits of precision after the decimal point are required.

Same example with dc (reversed polish calculator, lighter than bc):

    $ echo "3 k 10 3 / p" | dc

k sets the precision to 3, and p prints the value of the top of the stack with a newline. The stack is not altered, though.

If you are trying to compare floating point numbers, be aware that a simple x < y is not supported by all versions of bc.

    # This would work with some versions, but not HP-UX 10.20.
    # The here string feature, inherited from rc->zsh->ksh93 was
    # introduced in bash 2.05b-alpha1
    imadev:~$ bc <<< '1 < 2'
    syntax error on line 1,

Alternatively, you could use this:

    # Bash
    if [[ $(bc <<< "1.4 - 2.5") = -* ]]; then
        echo "1.4 is less than 2.5."
    fi

This example subtracts 2.5 from 1.4, and checks the sign of the result. If it is negative, the first number is less than the second.

Portable version:

    # Bourne
    case "`echo "1.4 - 2.5" | bc`" in
      -*) echo "1.4 is less than 2.5";;
    esac

AWK can be used for calculations, too:

    $ awk 'BEGIN {printf "%.3f\n", 10 / 3}'
    3.333

There is a subtle but important difference between the bc and the awk solution here: bc reads commands and expressions from standard input. awk on the other hand evaluates the expression as part of the program. Expressions on standard input are not evaluated, i.e. echo 10/3 | awk '{print $0}' will print 10/3 instead of the evaluated result of the expression.

Newer versions of zsh and the KornShell have built-in floating point arithmetic, together with mathematical functions like sin() or cos() .

Caveat: Many problems that look like floating point arithmetic can in fact be solved using integers only, and thus do not require these tools: Problems dealing with rational numbers. For example, to check if two numbers x and y are in a ratio of 4:3 or 16:9 you may use something along these lines:

# Variables x and y are integers
if (( $x*9-$y*16==0 )) ; then
   echo "16:9."
elif (( $x*3-$y*4==0 )) ; then
   echo "4:3."
else
   echo "Neither 16:9 nor 4:3."
fi

A more elaborate test could tell if the ratio is closest to 4:3 or 16:9 without using floating point arithmetic. Note that this very simple example that apparently involves floating point numbers and division is solved with integers and no division. If possible, it's usually more efficient to convert your problem to integer arithmetic than to use floating point arithmetic.

(*)Actually, I lied. It can print them, using printf and one of the %e or %f or %g format strings. But that's all.

BashFAQ/022 (last edited 2021-09-01 06:31:58 by geirha)