司在剧本和浮点 [英] Division in script and floating-point
问题描述
我愿做我的脚本如下操作:
I would like to do the following operation in my script:
1 - ((m-20) / 34)
我想分配此操作另一个变量的结果。我希望我的脚本中使用浮点运算。例如,对于m = 34:
I would like to assign the result of this operation to another variable. I want my script use floating point math. For example, for m = 34:
results = 1 - ((34-20) / 34) == 0.588
感谢您提前为你的答案。
Thank you in advance for your answers.
推荐答案
您可以使用 BC
计算器。它会使用小数(不是二进制浮点)进行任意precision数学,如果你设置increease 比例
从0默认:
You could use the bc
calculator. It will do arbitrary precision math using decimals (not binary floating point) if you set increease scale
from its default of 0:
$ m=34
$ bc <<< "scale = 10; 1 - (($m - 20) / 34)"
.5882352942
的 -l </ code>选项将加载标准的数学库和默认规模为20:
The -l
option will load the standard math library and default the scale to 20:
$ bc -l <<< "1 - (($m - 20) / 34)"
.58823529411764705883
您就可以用printf格式化输出,如果你选择:
You can then use printf to format the output, if you so choose:
printf "%.3f\n" $(bc -l ...)
这篇关于司在剧本和浮点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!