Subject: bug#54785: for floating point, printf should use double like in C instead of long double

From: Vincent Lefevre <[email-masked]>
Date: Sat, 30 Apr 2022 14:48:23 +0200

On 2022-04-29 16:16:28 -0700, Paul Eggert wrote:
> On 4/29/22 13:04, Chet Ramey wrote:
> > I think I'm going to stick with the behavior I proposed, fixing the POSIX
> > conformance issue and preserving backwards compatibility, until I hear more
> > about whether backwards compatibility is an issue here.
> 
> Come to think of it, as far as POSIX is concerned Bash doesn't need to
> change what it does. POSIX doesn't require that the shelll printf command be
> compiled with any particular environment. It would conform to POSIX, for
> example, if Bash's printf were compiled with an IBM floating point
> implementation rather than with an IEEE floating point implementation, so
> long as Bash's printf parses floating-point strings the way strtod is
> supposed to parse strings on an IBM mainframe. Similarly, Bash's printf can
> use an 80-bit floating point format if available; it will still conform to
> POSIX.

Yes, but to be clear, POSIX says:

  shall be evaluated as if by the strtod() function if the
  corresponding conversion specifier is a, A, e, E, f, F, g, or G

so the number should be regarded as a double-precision number
(type double). Then this number can be stored in a long double
since any double is representable exactly as a long double.

> So this isn't a POSIX conformance issue; only a compatibility issue. Is it
> more important for the Bash printf to behave like most other shells and
> other programs, or is it more important for Bash printf to behave like it
> has for the last 18 years or so?

Concerning the compatibility, the question is: with what?
  * If the goal is to communicate with other tools (e.g. zsh,
    XPath-based, but also programs that output values in decimal
    from double, which is the most common type used in practice),
    then double should be used.
  * If the goal is to communicate with other machines, then double
    is again preferable, since the long double type depends on the
    platform (x86, powerpc and aarch64 using 3 different formats).
  * Concerning just a bash script running on some machine:
      - If printf (without a length modifier) switches to double,
        the behavior will change.
      - Note that the behavior of the script on different platforms
        will be different if long double is used, but will be the
        same if double is used.

Note that since bash doesn't support FP arithmetic in its arithmetic
expressions, it is very probable that FP values provided to printf
come from other tools (first point above), thus are probably in
double precision.

-- 
Vincent Lefèvre <[email-masked]> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)