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

92 views
Skip to first unread message

Martin Eden

unread,
Jun 18, 2026, 4:37:56 AMJun 18
to lu...@googlegroups.com
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

-- Martin

Yao Zi

unread,
Jun 18, 2026, 11:08:01 AMJun 18
to 'Martin Eden' via lua-l, Yao Zi
"%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

Philippe Verdy

unread,
Jun 25, 2026, 6:04:48 PM (7 days ago) Jun 25
to lu...@googlegroups.com, Yao Zi
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).

--
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 lua-l+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/lua-l/ajQJtWv6snR_cYki%40pie.
Reply all
Reply to author
Forward
0 new messages