mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: Michal Terepeta <michalt@google.com>
Cc: musl@lists.openwall.com, t5y-external <t5y-external@google.com>
Subject: Re: [musl] Wrong formatting of doubles?
Date: Tue, 1 Jul 2025 10:22:25 -0400	[thread overview]
Message-ID: <20250701142223.GA1827@brightrain.aerifal.cx> (raw)
In-Reply-To: <CAKdOdAonsh8=ts2jU4ouz57F4BHV1jMrh0oJm9g1pbnQcaQrww@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 2318 bytes --]

On Tue, Jul 01, 2025 at 07:55:20AM +0200, Michal Terepeta wrote:
> Hi,
> 
> We're working on a RISC-V system and using musl as our libc. A recent change
> in musl seems to have caused wrong formatting (printf) of double values:
> https://git.musl-libc.org/cgit/musl/commit/src/stdio/vfprintf.c?id=572a2e2eb91f00f2f25d301cfb50f435e7ae16b3
> 
> Using a simple binary to print max double [1] with the current code I get:
> 
> ```
> The maximum value of a double (printf %e): 1.348676e+308
> The maximum value of a double (printf %E): 1.348676E+308
> The maximum value of a double (printf %g): 3.13486e+308
> ```
> 
> With the patch [2] that reverts most of the above change, I get the expected
> output:
> 
> ```
> The maximum value of a double (printf %e): 1.797693e+308
> The maximum value of a double (printf %E): 1.797693E+308
> The maximum value of a double (printf %g): 1.79769e+308
> ```
> 
> It'd be great if someone could take a look if they can also repro and perhaps
> revert the change?
> 
> Many thanks!
> 
> Michal
> 
> 
> 
> [1] Repro program:
> 
> ```
> #include <float.h>
> #include <stdio.h>
> 
> int main() {
>   printf("The maximum value of a double (printf %%e): %e\n", DBL_MAX);
>   printf("The maximum value of a double (printf %%E): %E\n", DBL_MAX);
>   printf("The maximum value of a double (printf %%g): %g\n", DBL_MAX);
>   return 0;
> }
> 
> ```
> 
> 
> [2] Patch to test:
> 
> ```
> diff --git a/src/stdio/vfprintf.c b/src/stdio/vfprintf.c
> index 010041ca6c..01004158bf 100644
> --- a/src/stdio/vfprintf.c
> +++ b/src/stdio/vfprintf.c
> @@ -180,12 +180,8 @@
> 
>  static int fmt_fp(FILE *f, long double y, int w, int p, int fl, int t, int ps)
>  {
> -       int bufsize = (ps==BIGLPRE)
> -               ? (LDBL_MANT_DIG+28)/29 + 1 +          // mantissa expansion
> -                 (LDBL_MAX_EXP+LDBL_MANT_DIG+28+8)/9  // exponent expansion
> -               : (DBL_MANT_DIG+28)/29 + 1 +
> -                 (DBL_MAX_EXP+DBL_MANT_DIG+28+8)/9;
> -       uint32_t big[bufsize];
> +       uint32_t big[(LDBL_MANT_DIG+28)/29 + 1          // mantissa expansion
> +               + (LDBL_MAX_EXP+LDBL_MANT_DIG+28+8)/9]; // exponent expansion
>         uint32_t *a, *d, *r, *z;
>         int e2=0, e, i, j, l;
>         char buf[9+LDBL_MANT_DIG/4], *s;
> ```

Could you try the attached patch?


[-- Attachment #2: printf_dbl_bug.diff --]
[-- Type: text/plain, Size: 1207 bytes --]

diff --git a/src/stdio/vfprintf.c b/src/stdio/vfprintf.c
index 76733997..2d2f4f3e 100644
--- a/src/stdio/vfprintf.c
+++ b/src/stdio/vfprintf.c
@@ -180,11 +180,10 @@ typedef char compiler_defines_long_double_incorrectly[9-(int)sizeof(long double)
 
 static int fmt_fp(FILE *f, long double y, int w, int p, int fl, int t, int ps)
 {
-	int bufsize = (ps==BIGLPRE)
-		? (LDBL_MANT_DIG+28)/29 + 1 +          // mantissa expansion
-		  (LDBL_MAX_EXP+LDBL_MANT_DIG+28+8)/9  // exponent expansion
-		: (DBL_MANT_DIG+28)/29 + 1 +
-		  (DBL_MAX_EXP+DBL_MANT_DIG+28+8)/9;
+	int max_mant_dig = (ps==BIGLPRE) ? LDBL_MANT_DIG : DBL_MANT_DIG;
+	int max_exp = (ps==BIGLPRE) ? LDBL_MAX_EXP : DBL_MAX_EXP;
+	int bufsize = (max_mant_dig+28)/29 + 1          // mantissa expansion
+	            + (max_exp+max_mant_dig+28+8)/9;    // exponent expansion
 	uint32_t big[bufsize];
 	uint32_t *a, *d, *r, *z;
 	int e2=0, e, i, j, l;
@@ -266,7 +265,7 @@ static int fmt_fp(FILE *f, long double y, int w, int p, int fl, int t, int ps)
 	if (y) y *= 0x1p28, e2-=28;
 
 	if (e2<0) a=r=z=big;
-	else a=r=z=big+sizeof(big)/sizeof(*big) - LDBL_MANT_DIG - 1;
+	else a=r=z=big+sizeof(big)/sizeof(*big) - max_mant_dig - 1;
 
 	do {
 		*z = y;

  reply	other threads:[~2025-07-01 14:22 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-01  5:55 Wrong formatting of doubles? Michal Terepeta
2025-07-01 14:22 ` Rich Felker [this message]
2025-07-01 16:19   ` Rich Felker
2025-07-01 16:37     ` Rich Felker
2025-07-01 17:02       ` Jeffrey Walton
2025-07-01 17:22       ` [musl] " Markus Wichmann
2025-07-01 17:34         ` Rich Felker
2025-07-02  1:46       ` Rich Felker
2025-07-02  7:56         ` [musl] " Michal Terepeta
2025-07-08  7:43           ` Michal Terepeta
2025-07-08 16:22             ` [musl] " Rich Felker
2025-07-02 14:58         ` Rich Felker
2025-07-03  4:31           ` Markus Wichmann

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250701142223.GA1827@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=michalt@google.com \
    --cc=musl@lists.openwall.com \
    --cc=t5y-external@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).