how do arithmetic operations with floating point no in shell script

This is a discussion on how do arithmetic operations with floating point no in shell script within the Linux Administration forums, part of the Linux Forums category; sir, how do arithmetic operations with floating point number shell script program expr only provide solution for whole numbers I ...


Go Back   Usenet Forums > Linux Forums > Linux Administration

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 07-16-2004
karthikeyan
 
Posts: n/a
Default how do arithmetic operations with floating point no in shell script

sir,
how do arithmetic operations with floating point number shell script program
expr only provide solution for whole numbers
I want to add floating no
moreover even the ulility like "bc" - calculator adds only whole numbers
Is there any utility to operate on floating point no
Help me
thks in adv
Reply With Quote
  #2 (permalink)  
Old 07-16-2004
Michael Heiming
 
Posts: n/a
Default Re: how do arithmetic operations with floating point no in shell script

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
NotDashEscaped: You need GnuPG to verify this message

In comp.os.linux.misc karthikeyan <thegreatkarthik@yahoo.co.in> suggested:
> sir,
> how do arithmetic operations with floating point number shell script program
> expr only provide solution for whole numbers
> I want to add floating no
> moreover even the ulility like "bc" - calculator adds only whole numbers


Works for me:

echo "scale=2; 2.51+2.51" | bc
5.02

Good luck

--
Michael Heiming (GPG-Key ID: 0xEDD27B94)
mail: echo zvpunry@urvzvat.qr | perl -pe 'y/a-z/n-za-m/'
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFA98ahAkPEju3Se5QRAmmrAJ9awmuvxWh9jEIj6cN13x +uY1qSKgCfZxWH
mseBh9BOlw4hdNnkhchDPlc=
=hQic
-----END PGP SIGNATURE-----
Reply With Quote
  #3 (permalink)  
Old 07-16-2004
moma
 
Posts: n/a
Default Re: how do arithmetic operations with floating point no in shellscript

karthikeyan wrote:
> sir,
> how do arithmetic operations with floating point number shell script program
> expr only provide solution for whole numbers
> I want to add floating no
> moreover even the ulility like "bc" - calculator adds only whole numbers
> Is there any utility to operate on floating point no


bc should understand decimal numbers
$ echo "1.2 - 3.4 " | bc
-2.3


> Help me
> thks in adv


You may browse to
http://www.futuredesktop.org/RedHat9-apt-get.html
and search for "Math" on that page.


// moma
http://www.futuredesktop.org :: The most relevant links at the top



Reply With Quote
  #4 (permalink)  
Old 07-17-2004
karthikeyan
 
Posts: n/a
Default Re: how do arithmetic operations with floating point no in shell script

sir,
Thks a lot for your valuable help.
Is there any possible option to go for floating point calculation in "expr"
help me
Thks in adv again

moma <moma@example.net> wrote in message news:<apQJc.3735$v04.53421@news2.e.nsc.no>...
> karthikeyan wrote:
> > sir,
> > how do arithmetic operations with floating point number shell script program
> > expr only provide solution for whole numbers
> > I want to add floating no
> > moreover even the ulility like "bc" - calculator adds only whole numbers
> > Is there any utility to operate on floating point no

>
> bc should understand decimal numbers
> $ echo "1.2 - 3.4 " | bc
> -2.3
>
>
> > Help me
> > thks in adv

>
> You may browse to
> http://www.futuredesktop.org/RedHat9-apt-get.html
> and search for "Math" on that page.
>
>
> // moma
> http://www.futuredesktop.org :: The most relevant links at the top

Reply With Quote
  #5 (permalink)  
Old 07-18-2004
Robert E A Harvey
 
Posts: n/a
Default Re: how do arithmetic operations with floating point no in shell script

thegreatkarthik@yahoo.co.in (karthikeyan) wrote in message news:<ecaee7af.0407170405.6a3bc843@posting.google. com>...
> sir,
> Thks a lot for your valuable help.
> Is there any possible option to go for floating point calculation in "expr"
> help me
> Thks in adv again


If you want better maths than the shell can handle, consider using perl instead.
Reply With Quote
  #6 (permalink)  
Old 07-19-2004
Robert Komar
 
Posts: n/a
Default Re: how do arithmetic operations with floating point no in shell script

In comp.os.linux.misc karthikeyan <thegreatkarthik@yahoo.co.in> wrote:
> how do arithmetic operations with floating point number shell script program
> expr only provide solution for whole numbers
> I want to add floating no
> moreover even the ulility like "bc" - calculator adds only whole numbers
> Is there any utility to operate on floating point no


Hi,
awk will do simple arithmetic with floating point numbers. For example:

robpc2>echo 1.1 2.2 | awk ' { printf "%f\n", $1 + $2 } '
3.300000

Cheers,
Rob Komar
Reply With Quote
  #7 (permalink)  
Old 07-19-2004
Scott Lurndal
 
Posts: n/a
Default Re: how do arithmetic operations with floating point no in shell script

robertharvey@my-deja.com (Robert E A Harvey) writes:
>thegreatkarthik@yahoo.co.in (karthikeyan) wrote in message news:<ecaee7af.0407170405.6a3bc843@posting.google. com>...
>> sir,
>> Thks a lot for your valuable help.
>> Is there any possible option to go for floating point calculation in "expr"
>> help me
>> Thks in adv again

>
>If you want better maths than the shell can handle, consider using perl instead.


ksh93 handles floating point calculations natively using the let or (( ))
expression evaluation functionality.

Available from <http://www.kornshell.com/>

scott
Reply With Quote
  #8 (permalink)  
Old 07-23-2004
Johan Kullstam
 
Posts: n/a
Default Re: how do arithmetic operations with floating point no in shell script

thegreatkarthik@yahoo.co.in (karthikeyan) writes:

> sir,
> how do arithmetic operations with floating point number shell script program
> expr only provide solution for whole numbers
> I want to add floating no
> moreover even the ulility like "bc" - calculator adds only whole
> numbers


You need to use an external program.

bc and dc are fine for really simple stuff.

awk (gawk) is good for medium level file scan - simple stuff on
command line, more complex things by script file. octave is good for
matrix math and can be operated by script files.

Other viable options include perl and clisp.

> Is there any utility to operate on floating point no
> Help me
> thks in adv


--
Johan KULLSTAM <kullstj-nn@comcast.net> sysengr
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 04:02 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0