bug-bash
[Top][All Lists]
Advanced

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

Re: REQUEST - bash floating point math support


From: Robert Elz
Subject: Re: REQUEST - bash floating point math support
Date: Wed, 05 Jun 2024 22:48:25 +0700

    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.





reply via email to

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