Re: [PATCH RFC v3 1/5] lib: kstrtox: support E notation in kstrtoudec64() and kstrtodec64()

From: Rodrigo Alencar

Date: Wed Oct 21 2026 - 05:43:22 EST


On 21/10/26 11:45, Andy Shevchenko wrote:
> On Tue, Oct 20, 2026 at 09:26:59AM -0300, Rodrigo Alencar via B4 Relay wrote:
> >
> > Allow decimal strings to carry an exponent suffix, such as "2.7e10" or
> > "271828e-5", in kstrtoudec64() and kstrtodec64(). The exponent is added
> > to the requested scale to work out how many integer and fractional digits
> > are consumed. A negative effective exponent drops integer digits. Digits
> > beyond the resulting precision are truncated, as before.
> >
> > The digits are now scanned first and the string termination is checked
> > before any conversion. A missing or malformed exponent ("1e", "1e+")
> > returns -EINVAL. An exponent that overflows when added to the scale, or
> > that makes the result exceed 64 bits, returns -ERANGE.

...

> > static int _kstrtoudec64(const char *s, unsigned int scale, u64 *res)
> > {
> > - unsigned int rv_int, rv_frac;
> > + const char *s_int, *s_frac;
> > + int len_int, len_frac;
> > + unsigned int rv;
> > u64 _res = 0;
> > + int exp = 0;
> > + int ret;
> >
> > - rv_int = _parse_integer(s, 10, &_res);
> > - if (rv_int & KSTRTOX_OVERFLOW)
> > - return -ERANGE;
> > - s += rv_int;
> > + s_int = s;
> > + len_int = _skip_digits(&s);
> >
> > if (*s == '.')
> > s++; /* skip decimal point */
>
> So, I know that '.NNN' is valid notation, but is 'NNN.'?

It looks weird, but that is also valid notation.

>
> > - rv_frac = _parse_integer(s, 10, &_res, scale, _res);
> > - if (rv_frac & KSTRTOX_OVERFLOW)
> > + s_frac = s;
> > + len_frac = _skip_digits(&s);
> > +
> > + if (!len_int && !len_frac)
> > + return -EINVAL;
> > +
> > + if (*s == 'e' || *s == 'E') { /* E Notation */
>
> You may use _tolower() helper (yes, the one with a single leading underscore).

ok, so we only check for _tolower(*s) == 'e'

> > + s++;
> > + ret = kstrtoint(s, 10, &exp);
> > + if (ret)
> > + return ret;
>
> So, with the above in mind, this may lead to 'NNN.e10' input which I find a bit
> confusing.

might be, but as mentioned, it is considered valid, e.g.:

- python:

>>> x = 2.e10
>>> print(x)
20000000000.0
>>> y = .5e-3
>>> print(y)
0.0005

- c++

int main()
{
float x = 2.e3;
std::cout<<"x = " << x << std::endl;

return 0;
}

x = 2000

--
Kind regards,

Rodrigo Alencar