Bash中具有浮点值的C风格算术 [英] C style arithmetic with floating point values in Bash

查看:86
本文介绍了Bash中具有浮点值的C风格算术的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从此bash脚本中获得正确的结果?

How can I have the right result from this bash script?

#!/bin/bash
echo $(( 1/2 ))

结果是0!因此,我尝试使用这些方法,但没有成功:

I get 0 as result! So I tried to use these but without success:

$ echo $(( 1/2.0 ))
bash: 1/2.0 : syntax error: invalid arithmetic operator (error token is ".0 ")
$ echo $(( 1.0/2 ))
bash: 1.0/2 : syntax error: invalid arithmetic operator (error token is ".0/2 ")

推荐答案

并不是单独使用 floats 的正确工具,您应该使用

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆