lua-users home
lua-l archive

tostring vs. nan, inf

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi, list!
As opposed to other numbers, for inf and nan tostring() returns values
that Lua can not read back:
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
> return tostring(1/0)
inf
> return tostring(-1/0)
-inf
> return tostring(0/0)
nan
> return assert(loadstring("return inf, nan"))()
nil	nil
> return assert(loadstring("return -inf"))()
[string "return -inf"]:1: attempt to perform arithmetic on global
'inf' (a nil value)
stack traceback:
	[string "return -inf"]:1: in main chunk
	(tail call): ?
	[C]: ?
This makes it harder to write serialization functions.
I do not think that it is worth the hassle of adding inf and nan
keywords to the language. (Also, one can simply define them as global
variables — which I find aesthetically unpleasant.)
But, maybe, change tostring() so it would return "1/0", "-1/0" and
"0/0" instead of inf, -inf and nan? Even the number of characters
would be the same. (Not sure though, if that would be a good thing for
the case when Lua is compiled with e.g. integer numbers.)
Any thoughts?
Alexander.

AltStyle によって変換されたページ (->オリジナル) /