bash 浮点运算_Bash Shell中的浮点

bash 浮点运算

Integers are natively supported in Bash shell. However, what if we use floating point in Bash shell?

Bash shell本机支持整数。 然而,如果我们使用浮什么中的Bash shell?

The short and direct answer is using bc‘ command – “An arbitrary precision calculator language.”

简短而直接的答案是使用bc ”命令 -“任意精度的计算器语言。”

Just run bc  and enter some floating point calculation expression, such as “1.2+8.2”, bc will give the result.

只需运行bc并输入一些浮点计算表达式,例如“ 1.2 + 8.2”,bc就会给出结果。

In a script, we certainly need a more automatic way. This is a piece of simple script:

在脚本中,我们当然需要一种更自动的方式。 这是一个简单的脚本:

$ echo "scale=4; $*" | bc -q

For example, to calculate “1.2+8.2”

例如,要计算“ 1.2 + 8.2”

$ echo "scale=4; 1.2+8.2" | bc -q

and you will get 9.4 .

您将获得9.4。

“By default bc outputs its result with no digits to the right of the decimal point and without a decimal point. To change this you have to change one of bc‘s builtin variables: scale. This is where the “language” features of bc are relevant, in bc as in C statements are separated by semi-colons.”

“默认情况下, bc输出的结果在小数点右边没有数字,并且没有小数点。 要更改此设置,您必须更改bc的内置变量之一: scale 。 这是与bc的“语言”功能相关的地方,在bc中,如C语句中的分号分隔。

For more detailed tutorial on bc, I suggest: Floating Point Math in Bash by Mitch Frazier.

有关bc的详细教程 ,我建议:Mitch Frazier撰写的Bash中浮点数学

翻译自: https://www.systutorials.com/floating-point-in-bash-shell/

bash 浮点运算

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值