[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: REQUEST - bash floating point math support
From: |
Saint Michael |
Subject: |
Re: REQUEST - bash floating point math support |
Date: |
Wed, 5 Jun 2024 13:31:20 -0400 |
the most obvious use of floating variables would be to compare
balances and to branch based on if a balance is lower than a certain
value
I use:
t=$(python3 -c "import math;print($balance > 0)")
and the
if [ "$t" == "False" ];then
echo "Result <= 0 [$t] Client $clname $clid Balance $balance"
fi
There must be a solution without Awk or Python or BC. Internal to bash
On Wed, Jun 5, 2024 at 11:49 AM Robert Elz <kre@munnari.oz.au> wrote:
>
> Date: Wed, 5 Jun 2024 11:09:45 -0400
> From: Greg Wooledge <greg@wooledge.org>
> Message-ID: <ZmB_ucLxt6N4ne5K@wooledge.org>
>
> | > to convert floats back into integers again, controlling how
> | > rounding happens).
> |
> | Ironically, that last one is the one we already *do* have.
>
> Yes, I know about printf (and while POSIX doesn't require that floats
> be supported in printf(1), all the implementations I am aware of, do) but:
>
> | As long as you're OK with "banker's rounding"
>
> that is expressly not "controlling how rounding happens" - applications
> dealing with floats sometimes want round to nearest (which is what is
> happening in printf - the tiebreaker algorithm when up or down are equally
> near might be relevant, but usually isn't), others want round down (towards 0,
> that is, simply discard the fractional part - that's easy in sh with ${v%.*},
> though you might need to deal with "-0" as the result - others round up
> (away from 0) (harder in sh, but achievable), others want round towards
> minint (ie: positives round down, negatives round up), and I suppose round
> towards maxint (the opposite) might occur sometimes too, though I don't
> think I've ever seen a use for that one.
>
> Most of this can be done, with some difficulty sometimes, but they
> really ought to be done with arithmetic functions - in fact, it is
> hard to imagine any real floating point work that can be done without
> the ability to define functions that can be used in an arithmetic
> context.
>
> My whole point is that as simple as it seems to "just add float support
> to shell arithmetic" might seem, it wouldn't end there, it is almost
> certainly better to just not go there. There are plenty of other
> languages that can work with floats - not everything needs to be a shell
> script using only shell primitives. Use the appropriate tool, don't
> just pick one, and make it try to be everything.
>
> kre
>
> I have considered all this as I once thought of adding float arith to
> the shell I maintain, and it took very little of this kind of thought
> process to abandon that without ever writing a line of code for it.
>
>
>
- Re: REQUEST - bash floating point math support, (continued)
- Re: REQUEST - bash floating point math support, Chet Ramey, 2024/06/21
- Re: REQUEST - bash floating point math support, Chet Ramey, 2024/06/12
- Re: REQUEST - bash floating point math support, Robert Elz, 2024/06/05
- Re: REQUEST - bash floating point math support, Greg Wooledge, 2024/06/05
- Re: REQUEST - bash floating point math support, Léa Gris, 2024/06/06
- Re: REQUEST - bash floating point math support, Robert Elz, 2024/06/05
- Re: REQUEST - bash floating point math support,
Saint Michael <=
- Re: REQUEST - bash floating point math support, Greg Wooledge, 2024/06/05
- Re: REQUEST - bash floating point math support, Saint Michael, 2024/06/05
- Re: REQUEST - bash floating point math support, Robert Elz, 2024/06/05
- Re: REQUEST - bash floating point math support, Zachary Santer, 2024/06/06