[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: bash is not capable of comparing of strings and real numbers

From: DennisW
Subject: Re: bash is not capable of comparing of strings and real numbers
Date: Tue, 8 Dec 2009 09:46:36 -0800 (PST)
User-agent: G2/1.0

On Dec 8, 7:11 am, Greg Wooledge <address@hidden> wrote:
> On Mon, Dec 07, 2009 at 05:08:02PM -0800, DennisW wrote:
> > Since printf understands floats (or acts like it does), you can use it
> > plus a little care and luck to do float comparisons in Bash:
> > [...]
> > $ printf -v a "%08.2f" $a
> > $ printf -v b "%08.2f" $b
> > $ [[ $a < $b ]] && echo true || echo false
> > false    # CORRECT
> > echo "$a $b"
> > 00147.10 00023.00
> While this is certainly clever, it requires some knowledge of the
> possible range of values being compared, so that one can construct a
> suitable printf format specifier.  Any unexpectedly large or small
> input would break it.
> Real floating-point comparisons require the services of an external
> program (such as bc or awk) since bash has no built-in support for it.
> (Or a loadable bash builtin, but very few people use those.)

That's where the "care and luck" come in. With some convoluted
gyrations, code could scale the width and precision factors. It's not
the most desirable way to do it, but as you say, it's "clever".

So how come ksh has float and Bash doesn't?

reply via email to

[Prev in Thread] Current Thread [Next in Thread]