help-bash
[Top][All Lists]
Advanced

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

Re: To round to the correct nearest number in a Linux basic calculator


From: Tapani Tarvainen
Subject: Re: To round to the correct nearest number in a Linux basic calculator
Date: Sun, 26 Sep 2021 13:25:12 +0300

On Sat, Sep 25, 2021 at 07:24:00PM -0400, Greg Wooledge (greg@wooledge.org) 
wrote:

> you NEVER EVER EVER EVER EVER under any circumstances run bc with a
> low "scale" as a substitute for rounding in a second step.

I have to agree with that, even if there are situations where it does
(mainly by luck) work correctly.

But this goes too far:

> you never run bc with a lower scale value.

There are good reasons for changing the scale, even if not many.

> The scale value doesn't determine the rounding of the final answer.  It's
> used to hold the intermediate values at *every* step along the way.

Yes.

> The more intermediate steps there are, the more wrong the answer
> becomes.

That is correct as long as "wrong" is understood in the pure
mathematical sense. But that's not always what's wanted.

For example:

* Emulating a program or a calculator with a specific (lower)
precision, when it is specifically desired to get exactly same
(though mathematically wrong) results.

* As a teaching tool for understanding how different expressions
(mis)behave with rounding.

* I recall a case where the precision of a calculation, including
intermediate steps, was explicitly specified in law. Doing it "too
accurately" would have been (legally) wrong.

However, you are absolutely right that scale should not be used to
round the final result, only when it is explicitly desired to round
the intermediate values. And the above examples notwithstanding that
is pretty rare.

It is perhaps worth mentioning that the accuracy of expressions in bc
is not determined solely by the scale variable. In particular, if any
term in a calculation already has a higher precision it will be
retained, so changing the scale after a calculation can't be used to
round the result either.

-- 
Tapani Tarvainen



reply via email to

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