0

我一般是 shell 和 linux 的初学者,这是一个 shell 算术问题,我不知道在终端中写什么来解决这三个方程。很抱歉,如果这似乎是一个不好的问题,我尝试了 echo 命令和expr,但它们都是错误的,并且有许多不同的错误,例如 '(' 、...附近的语法错误、E0F 以及许多其他不幸的错误。我希望有人会为我提供正确的命令。感谢您的帮助。我会记下我使用的终端代码,但我知道这些代码是错误的。

$ x=8
$ y=21
$ echo $((2*x**3 + sqrt(y/2))
bash: unexpected EOF while looking for matching ')'
bash: syntax error: unexpected end of file
$ echo $((2*x**3) + (sqrt(y/2)))
bash: command substitution: line 1: syntax error near unexpected token +'
bash: command substitution: line 1: `(2*x**3) + (sqrt(y/2))'
$ echo $((2*x**3)+(sqrt(y/2))
bash: unexpected EOF while looking for matching )'
bash: syntax error: unexpected end of file
$ echo $((2*x**3)+(sqrt(y/2)))
bash: command substitution: line 1: syntax error near unexpected token +(sqrt(y/2))'
bash: command substitution: line 1: `(2*x**3)+(sqrt(y/2))'
$ echo $((2x**3)+(sqrt(y / 2)))
bash: command substitution: line 1: syntax error near unexpected token +(sqrt(y / 2))'
bash: command substitution: line 1: (2x**3)+(sqrt(y / 2))'
4

1 回答 1

1

The shell is not the right tool to do floating point computations. It only does integer math and does not provide functions like square root.

However, the bc utility does both. It is an arbitrary-precision decimal arithmetic language and calculator.

$ bc
>>> scale=5
>>> sqrt(21)
4.58257
>>> scale=19
>>> sqrt(21)
4.5825756949558400065
>>> x=8
>>> y=21
>>> x+5
13
>>> x^2
64
>>> 2*x^2 - sqrt(y/2)
124.7596296507960698846
>>> Type Control-D to exit interactive bc.
$

Be sure to read the manual page for bc with man bc to understand all of its capabilities and limitations.

于 2021-05-25T06:29:20.420 回答