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

"'Yao Zi' via lua-l" <[email protected]> Thu, 18 Jun 2026 15:07:33 +0000
Newsgroups gmane.comp.lang.lua.general
Message-ID <ajQJtWv6snR_cYki@pie>
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.