Re: how to calculate fractions with let?

From: Chris F.A. Johnson (cfajohnson_at_gmail.com)
Date: 03/21/05


Date: 21 Mar 2005 17:44:47 GMT

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


Relevant Pages

  • Re: How does this work?
    ... I found this command. ... calc "2+2" and it gives 4 ... The above calcfunction, calling awk in double quotes to allow parameter expansion, replaces the shell variable $* with the arguments passed to calcso if you call calcthen ... just don't expect it to behave well if you pass it unexpected arguments! ...
    (comp.unix.shell)
  • Re: UPDATE: The quest for the equation library
    ... it will also fail to run on a 49g+ running the same version ... "every rule has an exception": ... even though "XModem server" was running on the calc). ... Mini-challenge for everyone else: What was the problem? ...
    (comp.sys.hp48)
  • Re: How does this work?
    ... I found this command. ... calc "2+2" and it gives 4 ... which awk does, and prints 9. ... the way you want (eg expand variables). ...
    (comp.unix.shell)
  • Re: how to run more faster?
    ... here`s my awk code: ... Use bc or calc instead. ... is he really wants to use AWK as a scripting ... The xgawk distribution contains an interface to the ...
    (comp.lang.awk)
  • Re: Division again
    ... On 2008-11-26, JAW wrote: ... Use awk, e.g.: ... calc 100 / 200 ...
    (comp.unix.shell)