Differences between revisions 30 and 31
Revision 30 as of 2008-12-02 10:18:07
Size: 368
Editor: ip70-188-119-42
Comment: 3.33333333333333
Revision 31 as of 2008-12-02 13:12:34
Size: 2210
Editor: GreyCat
Comment: spam
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
well, With all our love, Greetings from Holland and all the best ! When you come to Holland ( Assen ) in the future contact us and you can by free in a hotel ! I've just found your web site (not sure how now) and have spent the last 45 minutes reading it potatoes Interesting posts, besides those spam... http://tvsedv.sosblog.fr/ sandwich, Anyway, All the Best. <<Anchor(faq22)>>
== How can I calculate with floating point numbers instead of just integers? ==
[[BASH]] does not have built-in floating point [[ArithmeticExpression|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.

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()}}} .

(*)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.

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() .

(*)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)