From: Andy Shevchenko <andriy.shevchenko@intel.com>
To: rodrigo.alencar@analog.com
Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org,
Andrew Morton <akpm@linux-foundation.org>,
Jonathan Cameron <jic23@kernel.org>,
David Lechner <dlechner@baylibre.com>,
Andy Shevchenko <andy@kernel.org>
Subject: Re: [PATCH RFC v3 2/5] lib: kstrtox: round to nearest, ties to even in kstrto[u]dec64()
Date: Wed, 21 Oct 2026 12:03:25 +0300 [thread overview]
Message-ID: <ath_3QVy5DVV4fxw@ashevche-desk.local> (raw)
In-Reply-To: <20261020-e-notation-v3-2-fd385c0fbf65@analog.com>
On Tue, Oct 20, 2026 at 09:27:00AM -0300, Rodrigo Alencar via B4 Relay wrote:
> kstrtoudec64() and kstrtodec64() drop the digits beyond the requested
> scale, which truncates the result toward zero. The error is up to one
> unit in the last place and is always in the same direction.
>
> This is easy to hit with values produced by userspace floating point
> formatting. Printing 0.3 with "%.17g" gives "0.29999999999999999",
> which is parsed as 299999 with a scale of 6 instead of 300000. With E
> notation a negative exponent can also drop integer digits, so "19e-1"
> with a scale of 0 gives 1 instead of 2.
>
> Round the result to nearest instead, with ties to even. This is the
> IEEE 754 default rounding mode, as used by strtod() and printf(), and it
> does not bias results in either direction. The first discarded digit is
> known from where parsing stopped, so no extra pass over the string is
> needed: the remaining digits are only checked when the first discarded
> digit is a 5. Since round-half-even is symmetric, kstrtodec64() keeps
> rounding the magnitude and negates it.
>
> Inputs that fit within the scale are parsed exactly as before. An input
> that rounds up past the type limit, such as "18446744073.7095516155"
> with a scale of 9, now returns -ERANGE.
> Update the existing truncation test cases and add cases for ties and
> for rounding at the type limits.
...
> +static bool _round_half_even(const char *p, const char *end, u64 res)
> +{
> + if (p == end || *p < '5')
> + return false;
> + if (*p++ > '5')
This hidden ++ makes the whole if chain to be strict in order, gives less
readability and flexibility in my opinion.
> + return true;
So, this can be split to
// Does this need to be first?
if (p == end)
return false;
if (*p < '5')
return false;
if (*p > '5')
return true;
while (++p < end) {
...
}
> + /* tie: any nonzero digit after it breaks it, skipping the decimal point */
> + for (; p < end; p++)
> + if (*p != '0' && *p != '.')
This is a loop, so this also passes '.0.0.' case and some other similar to it.
Is this a problem?
> + return true;
> + return res & 1;
So, this res is used only once to get a parity. But this doesn't semantically
sound like boolean to me. What if we actually return 0 or 1 from this function
and use its output directly in the check_add_overflow()?
> +}
--
With Best Regards,
Andy Shevchenko
next prev parent reply other threads:[~2026-10-21 9:03 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-20 12:26 [PATCH RFC v3 0/5] Add E notation support to kstrtodec64() and IIO sysfs writes Rodrigo Alencar via B4 Relay
2026-10-20 12:26 ` [PATCH RFC v3 1/5] lib: kstrtox: support E notation in kstrtoudec64() and kstrtodec64() Rodrigo Alencar via B4 Relay
2026-10-21 8:45 ` Andy Shevchenko
2026-10-21 9:43 ` Rodrigo Alencar
2026-10-20 12:27 ` [PATCH RFC v3 2/5] lib: kstrtox: round to nearest, ties to even in kstrto[u]dec64() Rodrigo Alencar via B4 Relay
2026-10-21 9:03 ` Andy Shevchenko [this message]
2026-10-21 9:35 ` Rodrigo Alencar
2026-10-20 12:27 ` [PATCH RFC v3 3/5] lib: test-kstrtox: add E notation tests for kstrto[u]dec64() Rodrigo Alencar via B4 Relay
2026-10-20 12:27 ` [PATCH RFC v3 4/5] iio: core: use kstrtodec64() to parse fixed-point values Rodrigo Alencar via B4 Relay
2026-10-20 17:07 ` Rodrigo Alencar
2026-10-21 9:08 ` Andy Shevchenko
2026-10-20 12:27 ` [PATCH RFC v3 5/5] iio: test: add kunit tests for channel info writes Rodrigo Alencar via B4 Relay
2026-10-20 12:51 ` [PATCH RFC v3 0/5] Add E notation support to kstrtodec64() and IIO sysfs writes Rodrigo Alencar
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=ath_3QVy5DVV4fxw@ashevche-desk.local \
--to=andriy.shevchenko@intel.com \
--cc=akpm@linux-foundation.org \
--cc=andy@kernel.org \
--cc=dlechner@baylibre.com \
--cc=jic23@kernel.org \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=rodrigo.alencar@analog.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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®