[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: Coda Highland <chighland@...>
- Date: Sun, 8 Sep 2013 14:12:34 -0700
On Sun, Sep 8, 2013 at 1:48 PM, Dirk Laurie <dirk.laurie@gmail.com> wrote:
> 2013/9/8 Coda Highland <chighland@gmail.com>:
>
>> while tostring(tonumber(x)) can't be a guaranteed round-trip,
>> tonumber(tostring(x)) CAN be, by making tostring() output enough
>> digits.
>
> For a certain meaning of "enough", yes. Is "enough" 17 digits,
> though? Interesting but decidedly non-trivial question!
>
It's not non-trivial at all. It's actually a fairly well-known result.
17 digits is sufficient to uniquely identify every possible
double-precision floating point number. Certainly you can't precisely
express some values in 17 digits but no two double-precision IEEE
floats map to the same 17-digit number.
> Do we really want "tostring(0.1)" to return "0.10000000000000001"?
Yes, I think we do. If you wanted something to LOOK pretty, instead of
having it be CORRECT, you wouldn't use tostring() -- you'd be using
print.format().
/s/ Adam
- References:
- why tostring for number use LUA_NUMBER_FMT "%.14g", not "%.16g" ?, pulleyzzz_gmail
- Re: why tostring for number use LUA_NUMBER_FMT "%.14g", not "%.16g" ?, Leo Razoumov
- Re: why tostring for number use LUA_NUMBER_FMT "%.14g", not "%.16g" ?, Coda Highland
- Re: why tostring for number use LUA_NUMBER_FMT "%.14g", not "%.16g" ?, Dirk Laurie
- Re: why tostring for number use LUA_NUMBER_FMT "%.14g", not "%.16g" ?, Coda Highland
- Re: why tostring for number use LUA_NUMBER_FMT "%.14g", not "%.16g" ?, Dirk Laurie