[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: scientific notation in Bash
From: |
Ante Bilandzic |
Subject: |
Re: scientific notation in Bash |
Date: |
Tue, 15 Mar 2022 13:45:27 +0100 |
Thanks for the prompt feedback!
On Tue, Mar 15, 2022 at 1:23 PM Greg Wooledge <greg@wooledge.org> wrote:
> On Tue, Mar 15, 2022 at 01:03:54PM +0100, Ante Bilandzic wrote:
> > However, there seems to be the corner case, when I was expecting that
> Bash
> > will be able to handle the problem internally: the case when an integer
> is
> > written in a scientific notation, using 'e' or 'E'.
>
> As you have already observed, bash doesn't handle that. You would need
> to run some external utility (e.g. awk) to convert those into a form
> that bash can read.
>
> unicorn:~$ awk -v n=4.4e5 'BEGIN {printf("%d\n", n)}'
> 440000
> unicorn:~$ awk -v n=4.4e9 'BEGIN {printf("%d\n", n)}'
> 4400000000
>
>
There is a problem also with this solution, since:
$ mawk -v n=4.4e9 'BEGIN {printf("%d\n", n)}'
2147483647
$ gawk -v n=4.4e9 'BEGIN {printf("%d\n", n)}'
4400000000
Typically, I use awk in my scripts, which is then soft-linked either to
mawk or gawk on different machines.
My understanding is that Bash can internally handle very large integers,
e.g.
$ echo $((4400000000+1))
4400000001
So it would be great if this would be possible as well:
$ echo $((4.4e9+1))
4400000001
Thanks!
Cheers,
Ante