Unix questions - how to calculate fractions with let?

This is Interesting: Free IT Magazines  
Home > Archive > Unix questions > March 2005 > how to calculate fractions with let?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author how to calculate fractions with let?

2005-03-21, 6:03 pm

hi,
for the arithmetic expression 7/3, what unix function/command will show me the correct value of 2.33 and not just 2 like let?
thanks in advance,
-maurice
samuels@seas.upenn.edu
Chris F.A. Johnson

2005-03-21, 6:03 pm

On Mon, 21 Mar 2005 at 17:37 GMT, samuels@red.seas.upenn.edu () wrote:
> hi,
> for the arithmetic expression 7/3, what unix function/command will
> show me the correct value of 2.33 and not just 2 like let?


Most shells do not support fractional arithmetic (ksh93 is an
exception).

You need to use an external command such as awk or bc.

For example, I use this function:

calc()
{
awk 'BEGIN { OFMT="%f"; print '"$*"'; exit}'
}

Once it's defined you can use:

$ calc 7/3
2.333333


--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
========================================
===========================
My code (if any) in this post is copyright 2005, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License

2005-03-21, 6:03 pm

thanks :-)

Chris F.A. Johnson (cfajohnson@gmail.com) wrote:
: On Mon, 21 Mar 2005 at 17:37 GMT, samuels@red.seas.upenn.edu () wrote:
: > hi,
: > for the arithmetic expression 7/3, what unix function/command will
: > show me the correct value of 2.33 and not just 2 like let?

: Most shells do not support fractional arithmetic (ksh93 is an
: exception).

: You need to use an external command such as awk or bc.

: For example, I use this function:

: calc()
: {
: awk 'BEGIN { OFMT="%f"; print '"$*"'; exit}'
: }

: Once it's defined you can use:

: $ calc 7/3
: 2.333333


: --
: Chris F.A. Johnson http://cfaj.freeshell.org/shell
: ========================================
===========================
: My code (if any) in this post is copyright 2005, Chris F.A. Johnson
: and may be copied under the terms of the GNU General Public License
Rouben Rostamian

2005-03-21, 6:03 pm

In article <d1n0oq$ecsj$1@netnews.upenn.edu>,
<samuels@red.seas.upenn.edu> wrote:
>
>for the arithmetic expression 7/3, what unix function/command will
>show me the correct value of 2.33 and not just 2 like let?


echo "scale=6; 7/3" | bc
2.333333

echo "scale=30; 20/7" | bc
2.857142857142857142857142857142

--
Rouben Rostamian
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com