• shell的浮点运算


    参考: https://www.linuxquestions.org/questions/linux-software-2/multiply-floats-in-bash-script-618691/

    1.方法一: 用awk 来

    Quote:
    Originally Posted by mkrems View Post
    I have a bash script with the following line where $timestep is a decimal number.

    t=$timestep*$i 

    echo $t gives the value "0.125*2" for example instead of "0.250".
    How do I change this?!?
    bash does not have floating point arithmetic. Any variable with a decimal point in it is treated as a string. So bash processes t=$timestep*$i as the concatenation of three strings. The strings are:
    $timestep=0.125
    *
    $i=2

    You are not going to get bash to use 0.125 as a number in an arithmetic expression.

    --------------------
    Steve Stites
       
    Old 02-04-2008, 06:33 PM   #4
    colucix
    LQ Guru
     
    Registered: Sep 2003
    Location: Bologna
    Distribution: CentOS 6.5 OpenSuSE 12.3
    Posts: 10,509

    Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
     
    To do floating point calculations in a shell script you can invoke awk, e.g.
    Code:
    timestep=0.125
    i=2
    t=$(echo $timestep $i | awk '{printf "%4.3f
    ",$1*$2}'
    or you can use bc calculations (see man bc for details).

    2.方法二: 用bc命令

    hey guys, thanks for the help but actually i figured it out to be:

    t=$(expr $timestep*$i | bc)

    i have not tried the awk method, but the other ones did not work for me.

    It should be

    Code:
    t=$(echo $timestep*$i | bc)

    expr is a command who evaluates arithmetic expressions, while you have to simply echo the arithmetic expression to the bc calculator.

    -----------------------------------------------------------

    Bash 不能处理浮点运算, 并且缺乏特定的一些操作,这些操作都是一些重要的计算功能.幸运的是, bc  可以解决这个问题. bc  不仅仅是个多功能灵活的精确的工具, 而且它还提供许多编程语言才具备的一些方便的功能.   因为它是一个完整的 UNIX 工具, 所以它可以用在 管道 中,  bc  在脚本中也是很常用的.

    这里有一个简单的使用 bc 命令的模版可以用来在计算脚本中的变量. 用在命令替换 中.

    variable=$(echo "OPTIONS; OPERATIONS" | bc) 

    如:interest_rate=$(echo "scale=9; $interest_r/12 + 1.0" | bc)

     以前一直以为bc做了不了浮点运算,虽然他能结算类似
    13.4*45.6
    的乘法,但是在计算除法的时候,无论你输入
    5/3
    还是
    5/3.0
    得到的结果都是
    1

    我也没有去看man手册,今天无意中发现了ibase这个变量,是bc使用的一个变量,表示输入的数字的进制,比如ibase=8,表示你输入的数是8进制的。
    这让我很好奇,于是去看了man手册,原来他是可以做浮点除法的,只是默认不输出小数点后面的值,它同样采用了一个变量来控制--scale,其值表示输出多少位小数。另外一个和ibase对应的变量是obase,表示结果输出采用什么进制,默认是10进制。 
    给出几个例子,大家一看就明白了。

    [root@lancy bin]# echo "2.5*3.4" |bc
    8.5
    [root@lancy bin]# echo "5/3; 5/3.1" |bc
    1
    1
    [root@lancy bin]# echo "scale=2; 5/3" |bc
    1.66
    [root@lancy bin]# echo "ibase=10;obase=2; 4*6"|bc
    11000
    [root@lancy bin]# echo "ibase=2; 110*101; obase=10" |bc
    30
    [root@lancy bin]# echo "ibase=2; 11110; obase=2" |bc
    30

  • 相关阅读:
    centos7搭建nexus
    centos7搭建ftp
    tomcat笔记
    mysql跨服务器触发器
    IntelliJ IDEA笔记
    node.js笔记
    myeclipse笔记
    术语
    centos7安装mysql
    js跨域访问资源
  • 原文地址:https://www.cnblogs.com/oxspirt/p/9400585.html
Copyright © 2020-2023  润新知