On Mon, Sep 22, 2025 at 04:12:08 +0200, [email protected] wrote:
> $ fltexpr -p " 15 / 8 "
> 1.875
> 
> This is a nice answer.

This example is misleading in this context.  The value 15/8 can be
represented precisely in base 2.  There is no precision issue here at all.

1/10 cannot be stored precisely in base 2.  Any calculation that uses
1/10 as an input is by necessity using an approximation.  The answer
you get is therefore also an approximation.  You need to round it to
a sensible precision to get rid of the approximation errors.

But you *never* round a value until the very end, when you're printing
it for human consumption.  You always want to carry as much precision
as you can through all intermediate calculations.  Otherwise, the
errors balloon out of control.

> I what way would you tell Bash's printf to print it to you like that,
> when you don't know this answer beforehand?

hobbit:~$ printf '%.6g\n' 0.30000000000000004
0.3

%.6g happens to be what awk uses by default.  Maybe this is what you want.

Reply via email to