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

1.方法一: 用awk 来

Quote:
Originally Posted by mkrems 
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

   
 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: 
 
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\n",$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

shell的浮点运算的更多相关文章

  1. shell 浮点运算

    浮点运算 let 和 expr 都无法进行浮点运算,但是 bc 和 awk 可以. 范例:求 除以 ,保留 位有效数字 $ echo "scale=3; 1/13" | bc . ...

  2. shell简介

    Shell作为命令语言,它交互式地解释和执行用户输入的命令:作为程序设计语言,它定义了各种变量和参数,并提供了许多在高级语言中才具有的控制结构,包括循环和分支. shell使用的熟练程度反映了用户对U ...

  3. shell if 浮点数比较

    转shell中的浮点数比较http://nigelzeng.iteye.com/blog/1604640 博客分类: Bash Shell shell比较浮点数  由于程序需要,我要判断一个浮点数是否 ...

  4. shell 计算2

    转载 http://www.th7.cn/system/lin/201309/44683.shtml expr bc 在Linux下做算术运算时你是如何进行的呢?是不是还在用expr呢?你会说我还会b ...

  5. [Bash Shell] Shell学习笔记

    1. Shell简介 Shell本身是一个用C语言编写的程序,它是用户使用Unix/Linux的桥梁,用户的大部分工作都是通过Shell完成的.Shell既是一种命令语言,又是一种程序设计语言.作为命 ...

  6. Shell基础学习小结

    0 shell基础概念 Shell是解释性语言,使用脚本编程语言的好处是,它们多半运行在比编译型语言还高的层级,能够轻易处理文件与目录之类的对象:缺点是它们的效率通常不如编译型语言.Shell命令有本 ...

  7. 什么是shell

    Shell本身是一个用C语言编写的程序,它是用户使用Linux的桥梁.Shell既是一种命令语言,又是一种程序设计语言.作为命令语言,它交互式地解释和执行用户输入的命令:作为程序设计语言,它定义了各种 ...

  8. linux脚本编程(shell)浅介 (转载)

    linux脚本(shell)编程 啊,昨天上网看到一篇讲 linux/unix shell 的文章,想想自己最后写这东西也是一年前的事了,想想都快忘光了. 还是整理一下,做一次回顾,以后说不定还用得上 ...

  9. Shell编程基础

    写之前我们先来搞清楚为什么要学shell,学习要有目的性shell简单.灵活.高效,特别适合处理一些系统管理方面的小问题shell可以实现自动化管理,让系统管理员的工作变得容易.简单.高效shell脚 ...

随机推荐

  1. javascript高程笔记:逻辑与和逻辑或

    逻辑与和或 逻辑与 当 && 前后两个操作数都是布尔值,无可厚非,同时为true才为true.与其他强类型语言不同的是,javascript逻辑与前后的操作数可以应用于任何类型. 而且 ...

  2. App Center编译React Native平台Android应用

    做React Native一段时间后,对于React Native的发布有一些了解,原本的方法都是在本地直接生成APK文件的,具体可以参考<react native 生成APK> 因为需要 ...

  3. IO复用之epoll系列

    epoll是什么? epoll是Linux内核为处理大批量文件描述符而作了改进的poll,是Linux下多路复用IO接口select/poll的增强版本,它能显著提高程序在大量并发连接中只有少量活跃的 ...

  4. DaemonSet

    What is a DaemonSet? DaemonSet能够让所有(或者一些特定)的Node节点运行同一个pod.当节点加入到kubernetes集群中,pod会被(DaemonSet)调度到该节 ...

  5. bootstrap bootstrapvalidator插件+adjax验证使用

    1.利用bootstrap Validator表单验证进行表单验证需要如下CSS和JS. <link rel="stylesheet" type="text/css ...

  6. POJ 2777.Count Color-线段树(区间染色+区间查询颜色数量二进制状态压缩)-若干年之前的一道题目。。。

    Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 53312   Accepted: 16050 Des ...

  7. 关于php的session.serialize_handler的问题

    前言 php的session信息是储存在文件中的 session.save_path="" 指定储存的路径 session.save_handler="" 指定 ...

  8. 洛谷P4331 [BOI2004] Sequence 数字序列 [左偏树]

    题目传送门 数字序列 题目描述 给定一个整数序列 a1​,a2​,⋅⋅⋅,an​ ,求出一个递增序列 b1​<b2​<⋅⋅⋅<bn​ ,使得序列 ai​ 和 bi​ 的各项之差的绝对 ...

  9. python笔记一:函数的参数

    1.默认参数 def fun(x,y,z=3): sum=x+y+z return sum fun(1,2) 2.可变参数(两种方法定义) def fun(n): sum=0 for i in n: ...

  10. [BZOJ4765]普通计算姬(分块+树状数组)

    4765: 普通计算姬 Time Limit: 30 Sec  Memory Limit: 256 MBSubmit: 1725  Solved: 376[Submit][Status][Discus ...