在 bash 中计算功率

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23044728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:11:23  来源:igfitidea点击:

Calculating the power in bash

bash

提问by user2843457

I'm pretty new to bash scripting. I'm trying to work out calculations, and what I'm specifically trying to do is write a script that allows me to enter a parameter, and that my script calculates the power of 2 to that parameter.

我对 bash 脚本很陌生。我正在尝试进行计算,我特别想做的是编写一个允许我输入参数的脚本,并且我的脚本计算该参数的 2 次幂。

So say I would try

所以说我会尝试

bash scriptname 3

My script would calculate 2^3=8

我的脚本会计算 2^3=8

I'm trying with

我正在尝试

(( 2 ^  ))

but that's not doing anything. Is there a command to calculate the power of something that I'm not aware of?

但这并没有做任何事情。是否有命令可以计算我不知道的事物的力量?

回答by user000001

The power operator in bash is **

bash 中的幂运算符是 **

Example:

例子:

echo $((2 ** 4))
16

回答by devnull

It is worth pointing out that you'd observe overflow when the result starts to exceed LONG_MAX:

值得指出的是,当结果开始超过时,您会观察到溢出LONG_MAX

$ echo $((2**62))
4611686018427387904
$ echo $((2**63 - 1))
9223372036854775807
$ echo $((2**63))
-9223372036854775808

(Observe the result when the value exceeds 263-1)

(观察值超过2 63-1时的结果)

You might instead want to use something that allows arbitrary precision arithmetic since you might hit that limit rather fast if computing powers. For example, you could use bc(and it'd let you use ^too!):

您可能想要使用允许任意精度算术的东西,因为如果计算能力您可能会很快达到该限制。例如,您可以使用bc(它也可以让您使用^!):

$ bc <<< 2^63
9223372036854775808
$ bc <<< 2^128
340282366920938463463374607431768211456
$ BC_LINE_LENGTH=0 bc <<< 2^1024
179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216

回答by gniourf_gniourf

Just for fun you can also use bitwise shifts (of course this only works for a base that is a power of a power of 2):

只是为了好玩,您还可以使用按位移位(当然这仅适用于 2 的幂的基数):

echo $((1<<))

Make sure you read devnull's caveat about overflows.

请务必阅读 devnull 关于溢出的警告。

回答by Digital Trauma

The other answers are entirely correct, and if you are working with integer values represent the best way to do this kind of calculation. But it is worth noting that basharithmetic does not deal with non-integer values.

其他答案完全正确,如果您使用整数值,则代表进行此类计算的最佳方法。但值得注意的是,bash算法不处理非整数值。

If you require non-integer arithmetic like this, you can use the bcutility. e.g if you need to take the 3rd power of 2.5, you can do this:

如果您需要像这样的非整数算术,则可以使用该bc实用程序。例如,如果您需要取 2.5 的 3 次方,您可以这样做:

$ bc <<< "scale=10; 2.5 ^ 3"
15.625
$ 

Note setting the scalebuiltin variable sets the number of decimal places calculations should be given in.

注意设置scale内置变量设置计算应给出的小数位数。