Re: [Proposal] Support "inf", "-inf" and "-nan" in tonumber()

Philippe Verdy <[email protected]> Fri, 26 Jun 2026 00:04:29 +0200
Newsgroups gmane.comp.lang.lua.general
Message-ID <CAGa7JC1OKDc8oVN2pODA+OpU-An1fCDjiYz6vjLNAUBbW3E_Ug@mail.gmail.com>
IMHO these special formats returned by '%q' are just non-sense "hacks"
(they target the behavior of some specific current Lua parser versions, and
still the same parsers cannot handle "-nan", "-inf" and "+inf" returned by
the common tostring() serializer intended for humans and for interoperable
data formats).
What the Lua parser does for its own language syntax (and for the special
'%q' format) should not be exposed into tonumber()/tostring() (and the
common '%f' format) that should work interoperably (possibly with i18n
linguistic support).

Le jeu. 18 juin 2026 à 17:08, 'Yao Zi' via lua-l <[email protected]> a
écrit :

> On Thu, Jun 18, 2026 at 10:37:46AM +0200, 'Martin Eden' via lua-l wrote:
> > Current Lua implementation of tonumber() does not handle values
> > "inf", "-inf" and "-nan" for string that supposed to be number value.
> >
> > But tostring() function produces them:
> >
> >   Lua 5.5.0  Copyright (C) 1994-2025 Lua.org, PUC-Rio
> >   > tostring(1/0)
> >   inf
> >   > tostring(-1/0)
> >   -inf
> >   > tostring(0/0)
> >   -nan
> >
> > So tostring() value is useless for decoding number using tonumber():
> >
> >   > tonumber(tostring(1/0))
> >   nil
>
> "%q" specifier of string.format() guarantees the result could be safely
> read back by the interpreter, including inf and -inf ("1e9999" and
> "-1e9999" are returned respectively). However, for NaN it returns
> "(0/0)" which could be evaluated by interpreter but not parsed by
> tonumber(), either...
>
> > -- Martin
>
> Regards,
> Yao Zi
>
> --
> You received this message because you are subscribed to the Google Groups
> "lua-l" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion visit
> https://groups.google.com/d/msgid/lua-l/ajQJtWv6snR_cYki%40pie.
>

-- 
You received this message because you are subscribed to the Google Groups "lua-l" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To view this discussion visit https://groups.google.com/d/msgid/lua-l/CAGa7JC1OKDc8oVN2pODA%2BOpU-An1fCDjiYz6vjLNAUBbW3E_Ug%40mail.gmail.com.