[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Qemu-devel] [PATCH v9 01/37] qobject: Document more shortcomings in
From: |
Eric Blake |
Subject: |
Re: [Qemu-devel] [PATCH v9 01/37] qobject: Document more shortcomings in our number handling |
Date: |
Thu, 21 Jan 2016 10:12:41 -0700 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.5.0 |
On 01/20/2016 11:21 PM, Markus Armbruster wrote:
>> One alternative is to always output a guaranteed unambiguous decimal
>> string (although not necessarily the shortest), by using %.17f, using
>> <float.h> DBL_DECIMAL_DIG. (Note that DBL_DIG of 15 is NOT sufficient -
>> it is the lower limit that says that a decimal->float->decimal will not
>> change the decimal; but we want the converse where a
>> float->decimal->float will not change the float. There are stretches of
>> numbers where the pigeonhole principle applies; you can think of it this
>> way: there is no way to map all possible 2^10 (1024) binary values
>> inside 2^3 (1000) decimal digits without at least 24 of them needing one
>> more decimal digit. But by the same arguments, DBL_DECIMAL_DIG is an
>> upper limit and usually more than you need.)
>>
>> So, the question is whether we want to always output 17 digits, or
>> whether we want to do the poor-man's truncation scheme (easy to
>> understand, but not optimal use of the processor), or go all the way to
>> the algorithm of that paper (faster but lots harder to understand). For
>> reference, here's the poor-man's algorithm in pseudocode:
>
> I don't think we want to implement floating-point formatting ourselves.
Well, we already have _some_ level of floating-point support built in
for TCG emulation of floating point on various architectures. I don't
know how much would be easily reusable for this case, though.
>
>> if 0, inf, nan:
>> special case
>> else:
>> Obtain the DBL_DECIMAL_DIG string via sprintf %.17f
>> i = 17;
>> do {
>> truncate the original string to i-1 decimal characters
>> parse that with strtod()
>> if the bit pattern differs:
>> break;
>> } while (--i);
>> assert(i)
>> use i digits of the string
>
> That's a lot of strtod()... May not be noticable if we write the result
> to a slowish sink. Binary search could save a few.
>
> Naive idea: chop off trailing '0'?
That, and rounding trailing '9'. Any other digits in positions 1-16 are
significant (other digits in position 17 might not matter, though), so I
guess a full 17 iterations is probably not strictly necessary. Our
current code DOES chop trailing '0', but starting from the flawed
premise that 6 digits was enough precision.
>
>> As a separate patch, of course, but I have a pending patch that provides
>> a single place where we could drop in such an improvement:
>> https://lists.gnu.org/archive/html/qemu-devel/2015-12/msg03932.html
>
> Definitely separate.
Okay, all thoughts for a later day :)
--
Eric Blake eblake redhat com +1-919-301-3266
Libvirt virtualization library http://libvirt.org
signature.asc
Description: OpenPGP digital signature
[Qemu-devel] [PATCH v9 13/37] qom: Use typedef for Visitor, Eric Blake, 2016/01/19
[Qemu-devel] [PATCH v9 02/37] qapi: Avoid use of misnamed DO_UPCAST(), Eric Blake, 2016/01/19
[Qemu-devel] [PATCH v9 12/37] qapi: Don't cast Enum* to int*, Eric Blake, 2016/01/19