How to calculate arccos () in bash?

I need to calculate arccos()in a bash script.
gawk can calculate cos(theta)and sin(theta)
how to calculate arccos()in linux?

+4
source share
2 answers

Using the command line tool bcand referring to the Advantage Bash , the arccosine function is shown below, and $1is the first argument to the function arccos().

arccos ()
{
    scale=3
    if (( $(echo "$1 == 0" | bc -l) )); then
        echo "a(1)*2" | bc -l
    elif (( $(echo "(-1 <= $1) && ($1 < 0)" | bc -l) )); then
        echo "scale=${scale}; a(1)*4 - a(sqrt((1/($1^2))-1))" | bc -l
    elif (( $(echo "(0 < $1) && ($1 <= 1)" | bc -l) )); then
        echo "scale=${scale}; a(sqrt((1/($1^2))-1))" | bc -l
    else
        echo "input out of range"
        return 1
    fi
}
+7
source

You can do what you want by calling, for example, perl:

acos_05=`perl -E 'use Math::Trig; say acos(0.5)'`

, , bash? - , , bash - . , , , , ( ) - .

( ) : Python, Ruby, Tcl, Perl,...; - , bash.

+7

Source: https://habr.com/ru/post/1532016/


All Articles