Bash decimal arithmetic?
Nifty Hat Mitch
mitch48 at sbcglobal.net
Fri Nov 12 18:13:44 UTC 2004
On Thu, Nov 11, 2004 at 03:31:59PM +1100, Cameron Simpson wrote:
> Date: Thu, 11 Nov 2004 15:31:59 +1100
> From: Cameron Simpson <cs at zip.com.au>
> To: For users of Fedora Core releases <fedora-list at redhat.com>
> Cc:
> Subject: Re: Bash decimal arithmetic?
> Reply-To: cs at zip.com.au,
> For users of Fedora Core releases <fedora-list at redhat.com>
>
> On 22:18 10 Nov 2004, Graham Campbell <gc1111 at optonline.net> wrote:
> | On Wed, 2004-11-10 at 21:22 -0500, Dean Maluski wrote:
> | > Does bash have an option to do non integer math in scripts?
> | > I need my exact decimal numbers.
> |
> | An "exact decimal number" is a contradiction in terms. If you can
> | specify the precision you wish to retain then use bc to do the
> | calculations.
>
> Of course, an "exact decimal number" is not a contradiction in terms.
> 2.5 is exactly 2.5. or course, _some_ decimal numbers are not storable
> precisely in the _default_ internal binary format usually used for floats,
> but that doesn't make the noption nonsensical.
>
> But I'll second the suggestion that you use "bc" to do the work,
> possibly/probably controlled from a shell script.
>
Yep bc is cool here are two little scripts
f2c and c2f by way of example.
#!/bin/ksh
# f2c convert fahrenheit to centigrade.
bc << KELVIN
scale=3
(5 *(${1}-32 ))/ 9
KELVIN
#!/bin/ksh
# c2f convert centigrade to fahrenheit.
# Name by which this script was invoked.
program=`echo "$0" | sed -e 's/[^\/]*\///g'`
bc << KELVIN
scale=2
((9/5) * $1)+32
KELVIN
With this posting they are GPL with an original © 1989-2004 by me.
I have built a list of x2y scripts as I need them
Of interest for the exactitude folks 1 inch is exactly 2.54 cm.
--
T o m M i t c h e l l
spam unwanted email.
SPAM, good eats, and a trademark of Hormel Foods.
More information about the users
mailing list