[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: why tostring for number use LUA_NUMBER_FMT "%.14g", not "%.16g" ?
- From: Dirk Laurie <dirk.laurie@...>
- Date: Sun, 8 Sep 2013 17:58:13 +0200
2013/9/8 Coda Highland <chighland@gmail.com>:
> So there IS a valid question to be asked here: Why SHOULDN'T
> tostring() use %.16g?
What are you using tostring() for?
(1) To give something that satisfies tonumber(tostring(x)) == x.
(2) To print out only correct digits of the number that x is supposed
to represent.
Now, log(2^53,10)=15.95... I.e. almost, but not quite, 16 digits.
Close enough, though, that tha vagaries of decimal notation
don't allow you to distinguish in all cases between two different
values. I.e. you can't even achieve (1) with "%g.16".
The valid question, IMHO, should rather be: why does Lua not
have a variable called say 'string.numformat' which is initialiized
to "%.15g" but over which the user has control? But to this
question the answer is easy: because Lua already allows
local oldtostring = tostring
tostring = function(x) if type(x)=='number'
then return string.format("%.16g".x)
else return oldtostring(x)
end end