Shell basic teaching of floating point operations

Floating point arithmetic

Bash does not support floating-point operations. If you need to perform floating-point operations, you can use bc, awk.
method one:

echo "数字1运算符数字2" | bc      注意空格及符号

Insert picture description here
Insert picture description here

Method Two:

echo " 数字1运算符数字2 ">文件      注意空格及符号
 bc 文件

Insert picture description here
Insert picture description here
Method 1 and Method 2 have the same limitation, that is, multiplication and division cannot be accurate calculations. If you want accurate calculations, you can use method 3.
Method three:

echo "scale=有效数字;数字1算法数字2"|bc         注意空格及符号

Insert picture description here
Insert picture description here
Method four:

echo $(awk BEGIN'{print 数字1运算符数字2}')        注意空格及符号

Insert picture description here
You can see that awk can't do accurate calculations either

Guess you like

Origin blog.csdn.net/weixin_51613313/article/details/111270416