shell的浮点运算
参考: https://www.linuxquestions.org/questions/linux-software-2/multiply-floats-in-bash-script-618691/
1.方法一: 用awk 来

|
Quote:
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: You are not going to get bash to use 0.125 as a number in an arithmetic expression. -------------------- |
|
| #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 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
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的浮点运算的更多相关文章
- shell 浮点运算
浮点运算 let 和 expr 都无法进行浮点运算,但是 bc 和 awk 可以. 范例:求 除以 ,保留 位有效数字 $ echo "scale=3; 1/13" | bc . ...
- shell简介
Shell作为命令语言,它交互式地解释和执行用户输入的命令:作为程序设计语言,它定义了各种变量和参数,并提供了许多在高级语言中才具有的控制结构,包括循环和分支. shell使用的熟练程度反映了用户对U ...
- shell if 浮点数比较
转shell中的浮点数比较http://nigelzeng.iteye.com/blog/1604640 博客分类: Bash Shell shell比较浮点数 由于程序需要,我要判断一个浮点数是否 ...
- shell 计算2
转载 http://www.th7.cn/system/lin/201309/44683.shtml expr bc 在Linux下做算术运算时你是如何进行的呢?是不是还在用expr呢?你会说我还会b ...
- [Bash Shell] Shell学习笔记
1. Shell简介 Shell本身是一个用C语言编写的程序,它是用户使用Unix/Linux的桥梁,用户的大部分工作都是通过Shell完成的.Shell既是一种命令语言,又是一种程序设计语言.作为命 ...
- Shell基础学习小结
0 shell基础概念 Shell是解释性语言,使用脚本编程语言的好处是,它们多半运行在比编译型语言还高的层级,能够轻易处理文件与目录之类的对象:缺点是它们的效率通常不如编译型语言.Shell命令有本 ...
- 什么是shell
Shell本身是一个用C语言编写的程序,它是用户使用Linux的桥梁.Shell既是一种命令语言,又是一种程序设计语言.作为命令语言,它交互式地解释和执行用户输入的命令:作为程序设计语言,它定义了各种 ...
- linux脚本编程(shell)浅介 (转载)
linux脚本(shell)编程 啊,昨天上网看到一篇讲 linux/unix shell 的文章,想想自己最后写这东西也是一年前的事了,想想都快忘光了. 还是整理一下,做一次回顾,以后说不定还用得上 ...
- Shell编程基础
写之前我们先来搞清楚为什么要学shell,学习要有目的性shell简单.灵活.高效,特别适合处理一些系统管理方面的小问题shell可以实现自动化管理,让系统管理员的工作变得容易.简单.高效shell脚 ...
随机推荐
- WordPress主题设置插件,让你的站点有电脑、手机双主题
我们建站的时候总是会优先考虑自适应的主题,但是与之对应,免费的自适应主题都调用国外公共资源,访问速度不太理想.加上wordpress未经优化之前,本身也没有极高的访问效率.所以大家可以下载这款“主题设 ...
- python学习第一天_环境的搭建
Python linux环境的安装: 1.https://www.python.org/ftp/python/ 大家可以在这里下载自己所需的linux下的版本 ,这里我下载的2.6.6版本: 2.在C ...
- go chapter 5 - 异常处理 error、panic、recover
https://blog.csdn.net/tennysonsky/article/details/78946265 error(不中断).panic(中断).recover(拦截中断 类似于 ca ...
- Flask实战第63天:评论布局和功能实现
评论后端逻辑实现 设计评论模型表, 编辑apps.models.py class CommentModel(db.Model): __tablename__ = 'comment' id = db.C ...
- [BZOJ3167][P4099][HEOI2013]SAO(树形DP)
题目描述 Welcome to SAO ( Strange and Abnormal Online).这是一个 VR MMORPG, 含有 n 个关卡.但是,挑战不同关卡的顺序是一个很大的问题. 有 ...
- Problem B: 颠倒字符串
#include<stdio.h> #include<string.h> //用来调用strlen(str)函数 int main() { int i,n; ]; while( ...
- [转]MySQL更改用户密码
grant all privilegeson *.* to root@'localhost'identified by 'root'with grant option; grant all privi ...
- 认识udev
转:http://www.360doc.com/content/11/0415/21/1317564_109923795.shtml 因为本身从事存储行业,在工作中多次碰到用户有这样的要求:我的lin ...
- js模板引擎-art-template常用
art-template javascript 模板引擎 分为原生语法和简洁语法,本文主要是讲简洁语法 基础数据渲染 输出HTML 流程控制 遍历 调用自定义函数方法 子模板引入 基础数据渲染 一.引 ...
- django--02 模板的使用
参考链接:https://www.w3cschool.cn/django/django-template.html 1.新建模板目录 templates 2.在setting文件中注册模板 修改 T ...


