如何在bash中计算arccos()?

我需要在bash脚本中计算arccos()
gawk可以计算cos(theta)sin(theta)
如何在Linux中计算arccos()

通过使用命令行工具bc并引用Advantage Bash ,arccosine函数如下所示,而$1arccos()函数的第一个参数。

 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 } 

你可以通过调用perl来做你想做的事情,例如:

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

但是,正如michas指出的,你为什么要在bash中这样做呢? 如果你需要做的不仅仅是增加和增加数字,而且bash也不是工作的工具。 它从来没有被设计成这样做,它缺乏内在的功能,最重要的是,它的引用行为(把所有东西都当作字符串)把它做成在实践中完成任何事情的痛苦。

我推荐你选择的编程语言(不是shell脚本语言):Python,Ruby,Tcl,Perl,…; 他们都是比bash更好的语言。