Discussion:
[Help-bash] floating point arithmetic
Jerry
2017-10-30 20:19:38 UTC
Permalink
I was told that "bash" does not support floating point arithmetic. I was
wondering if there are any plans to change that in future releases?

Thanks!
--
Jerry
John McKown
2017-10-30 20:38:06 UTC
Permalink
Post by Jerry
I was told that "bash" does not support floating point arithmetic. I was
wondering if there are any plans to change that in future releases?
​Gee, I never knew that. My work around would be something like:

$ x=1.1
$ echo $x
1.1
$ y=2.2
$ echo $y
2.2
$ z=$(echo "${x} + ${y}" | bc)
$ echo $z
3.3

Post by Jerry
Thanks!
--
Jerry
--
I have a theory that it's impossible to prove anything, but I can't prove
it.

Maranatha! <><
John McKown
Greg Wooledge
2017-10-30 20:41:07 UTC
Permalink
Post by Jerry
I was told that "bash" does not support floating point arithmetic. I was
wondering if there are any plans to change that in future releases?
See also <http://mywiki.wooledge.org/BashFAQ/022>
John McKown
2017-10-30 20:48:00 UTC
Permalink
Post by Greg Wooledge
Post by Jerry
I was told that "bash" does not support floating point arithmetic. I
was
Post by Jerry
wondering if there are any plans to change that in future releases?
See also <http://mywiki.wooledge.org/BashFAQ/022>
​Very nice!​
--
I have a theory that it's impossible to prove anything, but I can't prove
it.

Maranatha! <><
John McKown
Barry Walker
2017-10-30 21:04:47 UTC
Permalink
Hi Jerry...
Post by Jerry
I was told that "bash" does not support floating point arithmetic. I was
wondering if there are any plans to change that in future releases?
If you are running CygWin then bc and dc are NOT part of a default install.
However all *NIX type systems have awk which can do floating point along with
some mathematical functions...

man awk:

https://www.freebsd.org/cgi/man.cgi?query=awk&sektion=1
--
73...

Bazza, G0LCU...

Team AMIGA...

The less that I speak, the smarter I sound.
João Eiras
2017-10-31 00:32:26 UTC
Permalink
I have my own little bash script for this use case:

$ cat calc
IFS=""
awk "BEGIN { print (${*:-0}); }" /dev/null

$ calc "2.3-5.4"
Post by Barry Walker
Hi Jerry...
Post by Jerry
I was told that "bash" does not support floating point arithmetic. I was
wondering if there are any plans to change that in future releases?
If you are running CygWin then bc and dc are NOT part of a default install.
However all *NIX type systems have awk which can do floating point along with
some mathematical functions...
https://www.freebsd.org/cgi/man.cgi?query=awk&sektion=1
--
73...
Bazza, G0LCU...
Team AMIGA...
The less that I speak, the smarter I sound.
Jerry
2017-10-31 09:38:23 UTC
Permalink
Thanks to everyone who replied. I appreciate it. However, I am still wondering
if Bash will ever support true floating point arithmetic natively? I was
advised to switch to "ksh93"; however, I have been trying hard to learn bash,
and I really don't want to switch if not absolutely necessary.

Thanks again, I got some good pointers.
--
Jerry
Chet Ramey
2017-10-31 20:18:25 UTC
Permalink
Post by Jerry
Thanks to everyone who replied. I appreciate it. However, I am still wondering
if Bash will ever support true floating point arithmetic natively? I was
advised to switch to "ksh93"; however, I have been trying hard to learn bash,
and I really don't want to switch if not absolutely necessary.
I don't think the class of problems that native floating point arithmetic
would solve (as opposed to using something like awk/bc/dc) is large
enough to compensate for the development effort. If someone else wanted
to take a crack at it, though, I'd most likely incorporate the results.
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU ***@case.edu http://cnswww.cns.cwru.edu/~chet/
Chris F.A. Johnson
2017-10-31 21:08:33 UTC
Permalink
Post by João Eiras
$ cat calc
IFS=""
awk "BEGIN { print (${*:-0}); }" /dev/null
I use this:

calc()
{
awk 'BEGIN {print '"${*//x/*}"'}'
}

...so that I can use 'x' instead of '*', which needs quoting.
--
Chris F.A. Johnson, <http://cfajohnson.com>
Loading...