Date:        Wed, 5 Jun 2024 13:31:20 -0400
    From:        Saint Michael <vene...@gmail.com>
    Message-ID:  
<CAC9cSOBMN3rW2gBEbRAoB6n_Yim8Xp=8u1nitjdhxj948zd...@mail.gmail.com>

  | 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

In addition to what Greg suggested, for the very simple case you
outlined ---

That's a perfect case for scaled integers - no-one ever deals with
fractions of cents in this kind of thing (a bank won't ever tell you
that your balance is $5678.17426 for example, even if the interest
calculations computed accurately might arrive at that number.)

So, just do the calculations/tests using cents instead of dollars and
fractional dollars (aka cents) (or pounds & pence, or Euros and whatever,
or ...)

So just

        dbal=${balance%.*}
        cbal=${balance#*.}
        case ${cbal} in ?) cbal=${cbal}0;; esac # just in case balance is 100.5
        cents=${dbal}${cbal}

If you need to deal with European notations, use [.,] in each place
instead of just . (From the way you used inserted $balance into the
python script, it is clear there are no grouping characters in the
value - ie: not 1,234.56 or 1 234,56 or similar.)

If sometimes the balance has no cents, either ends in '.' or no '.' at all,
rather than explicitly having .00 at the end, a few extra lines will handle
that as well.   You could also easily add more error/sanity checking on
the input values, if needed.

Then you can just use $cents and do integer arithmetic everywhere (and to
print it, if unchanged just use $balance, otherwise use the technique I
described in the previous message, except the scale factor is just 100,
so you want %.2d to print ${cents}%100 - etc).

Money is one of the most obvious cases where floating point isn't needed.

Not only because it is easy to work using the least valuable currency unit,
as above, but also because the calculations tend to be trivial, add, subtract,
and some simple multiplication (add tax at N% or whatever), and that's
about it.   No-one takes square roots, or ln() or whatever of their bank
balance, or required credit card payment.   But start doing floating point
and you'll soon get people saying "what do you mean I need to use bc to
calculate the natural log of ...?   Why can't I just do that in bash?".

And of course, soon to be followed by "now we have floating point numbers,
we need complex numbers and arithmetic as well".

Further, if you're going to use something like python in your
script to work on some aspect, why not just write (almost) all
of the code in python?  Sometimes you can make a program in
another language (like python) but where some operations there
are harder to achieve than in shell, where it is reasonable to
combine the two together - have the shell script do whatever is
needed to make it easy for python to do most of the work, then
when that's done, the script can do any required cleanup, etc.
Sometimes even several small python programs linked together by
a shell script might work.

[I wouldn't do that, I detest python, but I would use other similar
systems for the same effect.]

And last "There must be" is not an argument for absolutely anything
unless you can demonstrate why that is so.   Nothing you cannot
prove "must be".

kre

ps: and why would floating point be the thing to consider adding,
why not URLs as file names?  Surely bash should be able to do

        cat < https://some.host/path/to/file > mailto:bug-bash@gnu.org

Why do we need external programs for network access - something far
more likely for a shell script to want to do that real number arithmetic.

And of course, no, this is not a serious suggestion.


Reply via email to