From patchwork Mon Aug 24 22:16:11 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matt Turner X-Patchwork-Id: 142127 X-Patchwork-Delegate: azanella@linux.vnet.ibm.com Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4D3F54BA798C for ; Mon, 24 Aug 2026 22:18:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4D3F54BA798C Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=f6RrI+5a X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-yx1-xb12c.google.com (mail-yx1-xb12c.google.com [IPv6:2607:f8b0:4864:20::b12c]) by sourceware.org (Postfix) with ESMTPS id 274984BA9029 for ; Mon, 24 Aug 2026 22:16:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 274984BA9029 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 274984BA9029 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::b12c ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1787609790; cv=none; b=TVciInISjUTZniERpzRKpn3C1dYp2AuExUwBSRQFDccVQZ66rn/MOH71+F7fvVF0OnGp89nt6f31fD+xNIiwOQHR/VvspNQgNJ7R+wUrf1us71QllsowO8W9Mi9sh2d57BqsbHom4/mFlexbfXTBCpIaT7Vcl387LQJqenSjeIo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1787609790; c=relaxed/simple; bh=9Q0LV0um4ezkVzgpV9c7eRdz+6X8PsQsQ6qeDpfEUrY=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Ew/l9dLgUTSs2kV/hBa3w/e5Aykbr4TMPdNHk5kRWq89Sexqycsuy1JnJD1KLSmHE+lg+IBqmlimZ+LLcNxSD9pBr3LQqb3xtQ4vafTNLPZPmtzQlUFB8hVZRZNCEgEGBXVoK/o1e0rcEqFDm7mPo+22wre9QQzGRp/8eBA8Mrw= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=f6RrI+5a DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 274984BA9029 Received: by mail-yx1-xb12c.google.com with SMTP id 956f58d0204a3-66c82b32121so6049818d50.1 for ; Mon, 24 Aug 2026 15:16:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1787609789; x=1788214589; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=7BauixMcepc6/5KutIkIqBbXUYvFwj64pQw6kNIDJtE=; b=f6RrI+5aVjkYIIVq+wBwDQE++C7AmPrC1comPB9XiGHV2C0MO+4CkISDOLz/mgy/Y2 KFoKybAiY6DL4rjeIVZXabYzRFHAy3r1NW8g92rTcELccJr+eFz0tjq/RRCemaiiy9hv bUWN+gqdXmDolwuiych8rSQ96QMAKKnM2NlFLhV7616fLxrFbTbChAtQ1/utOVFg8imT drKfczlB7MB7RlGv354MJUor/q1dhP6HiGtmtR2nlAk2wawsEiMSV4/ReqRjoV+FjIpG vvMOJaWv1ZLO+uAcbElaFSNrc1JjNFf7rujtYxAjFGMJ8JRuVuhDtp3FAA85MBgjLVXz CQEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787609789; x=1788214589; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=7BauixMcepc6/5KutIkIqBbXUYvFwj64pQw6kNIDJtE=; b=UBcruhhiZmGBMSwMWkZ8EYtP+v0SqFwkfFFR8hoguyBrPyHDpEM7+2+zkYz0798aI6 fNsXE6p9b8KeTPAye13klDlGsM6R3AwO3c+fv+o/Drtyf5+28/jvfmrixUs9bfASS5iO pbSYLomIQqOeJ/kG9k2PN1BAfci0yliRfZWPdhIQP9g9QOKu/eEkEohmhs6VQJ1BjkEn i3EbFt+B8gDAGFpJMuo/jRabuqairrRBkIcnTMWNMz2aECgVCjMbHqN1QzXtrcs4zm+A /0tQ660DamJ8GuCbMyPDHJAC21a4ltmodputoB9rzhzznFg/Ra73Z/QrPT6c39mnx0nE Z30g== X-Gm-Message-State: AFuF++lpmtTIDPY0Hee44qX76WnosBjlenqoM0JiexggDLgXyHe7kg5F eD8k1O5YreWOGoYzjcnfAqK7bQOrJ9YtFPPASdGxiPbhQ4/ZGyfXh3X1sfnskQ== X-Gm-Gg: AR+sD10qNqxzVeb4wymjCwKU4aFAfPc5YM7ixOs6s+ZCHAnG1vvT4EcGI+7vrH4JFWc 1thHccT9wzo1iEdpnFlGhLJHReityM62Abz8C6wi6JIwYZ0stQ5oQCeNxrd5KvIN6B2ffFrCVNo M3pfXgCr4XUACT59pP7cRU/cf1zF6Wg8r40F2a7ez2YP3fYFETUVuPjhX7qO/BUFv+W6paQM4NI bXCgYSkTEQNGnit81oECM5DjvnvDBdJYtC1UMMGMquTkRkFwKAZnnQYS6N+ca+/OFzBQfZqdQaL KnBfnjNrUuTXshNKo2JKidE7h2JJV1ZJQRn6R8NPi6+kfC2SG94VYTcV269Qn/H2Hs9hdr/lrxs uLkLwbagJ0xhCsf0D9mKoH3xfrD1pJ3Q4NB2OSHK5iDvzOAiXnSX9Hmzum9NBcLE3iiJF6q6pWJ 9mbp2rQj3iU46DWseprTTeTf5WWbvxzqxdY/qHIwN/3VU5h/QJgbZKCzk0 X-Received: by 2002:a05:690e:144c:b0:66c:f6a3:32a5 with SMTP id 956f58d0204a3-66cf6a332d6mr6611045d50.15.1787609789330; Mon, 24 Aug 2026 15:16:29 -0700 (PDT) Received: from localhost ([67.197.117.177]) by smtp.gmail.com with ESMTPSA id 00721157ae682-853ff053a84sm10032657b3.4.2026.08.24.15.16.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Aug 2026 15:16:28 -0700 (PDT) From: Matt Turner To: libc-alpha@sourceware.org Cc: Matt Turner Subject: [PATCH 6/7] stdio-common: Keep trailing zeros where %#g rounds into a new decade Date: Mon, 24 Aug 2026 18:16:11 -0400 Message-ID: <20260824221612.4132669-7-mattst88@gmail.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260824221612.4132669-1-mattst88@gmail.com> References: <20260824221612.4132669-1-mattst88@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-10.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_PBL, SPF_HELO_NONE, SPF_PASS, TXREP shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org The g and G conversions choose between the f and e styles according to the exponent the value has once rounded to the requested number of significant digits. Where the value is small enough for the f style but rounding then carries into a new decade, printf_fp rewrites the digits it has already produced into the e style, recomputing along the way how many fractional digits the leading digit now leaves room for. FRACDIG_MIN, which the alternative form sets to the number of fractional digits that have to be retained rather than stripped, was left behind at the value computed for the f style. Where the value filled the whole integer part that is zero, so all the fractional digits were then stripped from a result the '#' flag requires to keep them: printf ("%#.2g", 99.9) gave "1.e+02" rather than "1.0e+02" printf ("%#g", 999999.9) gave "1.e+06" rather than "1.00000e+06" Update FRACDIG_MIN along with FRACDIG_MAX. Without the alternative form nothing retains trailing zeros, so the outcome is unchanged there. None of the values the conversion tests iterate over round this way, so add one that does at a precision they cover. Tested on x86_64-linux-gnu. Reviewed-by: Adhemerval Zanella --- stdio-common/printf_fp.c | 5 +++++ stdio-common/tst-printf-format-skeleton-double.c | 2 +- stdio-common/tst-printf-format-skeleton-ldouble.c | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git ./stdio-common/printf_fp.c ./stdio-common/printf_fp.c index b8f8c5fc07..97bd6afda3 100644 --- ./stdio-common/printf_fp.c +++ ./stdio-common/printf_fp.c @@ -896,6 +896,11 @@ __printf_fp_buffer_1 (struct __printf_buffer *buf, locale_t loc, fracdig_no += intdig_no; intdig_no = 1; fracdig_max = intdig_max - intdig_no; + if (info->alt) + /* The alternative form retains trailing zeros, and + the number of fractional digits it has to retain + has just changed along with FRACDIG_MAX. */ + fracdig_min = fracdig_max; ++p.exponent; /* Now we must print the p.exponent. */ p.type = isupper (info->spec) ? 'E' : 'e'; diff --git ./stdio-common/tst-printf-format-skeleton-double.c ./stdio-common/tst-printf-format-skeleton-double.c index ae3acb00f7..333bd568a2 100644 --- ./stdio-common/tst-printf-format-skeleton-double.c +++ ./stdio-common/tst-printf-format-skeleton-double.c @@ -30,7 +30,7 @@ typedef double type_t; static const type_t vals[] = { -HUGE_VAL, -DBL_MAX, -DBL_MIN, -0.0, -NAN, NAN, 0, DBL_TRUE_MIN, - DBL_MIN, DBL_MAX, HUGE_VAL }; + DBL_MIN, 99.9, DBL_MAX, HUGE_VAL }; static const char length[] = ""; #include "tst-printf-format-skeleton.c" diff --git ./stdio-common/tst-printf-format-skeleton-ldouble.c ./stdio-common/tst-printf-format-skeleton-ldouble.c index e323d621bb..0c828ec3b5 100644 --- ./stdio-common/tst-printf-format-skeleton-ldouble.c +++ ./stdio-common/tst-printf-format-skeleton-ldouble.c @@ -31,7 +31,7 @@ typedef long double type_t; static const type_t vals[] = { -HUGE_VAL, -LDBL_MAX, -LDBL_MIN, -0.0, -NAN, NAN, 0, LDBL_TRUE_MIN, - LDBL_MIN, LDBL_MAX, HUGE_VAL }; + LDBL_MIN, 99.9L, LDBL_MAX, HUGE_VAL }; static const char length[] = "L"; #ifndef TIMEOUT