Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NAi58-0004hr-Qw for qemu-devel@nongnu.org; Wed, 18 Nov 2009 05:48:14 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1NAi53-0004fZ-ES for qemu-devel@nongnu.org; Wed, 18 Nov 2009 05:48:13 -0500 Received: from [199.232.76.173] (port=45793 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NAi53-0004fW-7M for qemu-devel@nongnu.org; Wed, 18 Nov 2009 05:48:09 -0500 Received: from mx1.redhat.com ([209.132.183.28]:2303) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1NAi52-0006QO-OS for qemu-devel@nongnu.org; Wed, 18 Nov 2009 05:48:09 -0500 From: Markus Armbruster Subject: Re: [Qemu-devel] [PATCH 3/3] Add test suite for json marshalling References: <1257967478-4847-1-git-send-email-aliguori@us.ibm.com> <1257967478-4847-3-git-send-email-aliguori@us.ibm.com> <20091113031431.GF19405@shareable.org> Date: Wed, 18 Nov 2009 11:48:03 +0100 In-Reply-To: <20091113031431.GF19405@shareable.org> (Jamie Lokier's message of "Fri, 13 Nov 2009 03:14:31 +0000") Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Jamie Lokier Cc: Anthony Liguori , qemu-devel@nongnu.org, Luiz Capitulino Jamie Lokier writes: > Anthony Liguori wrote: >> After checking that we can demarshal, marshal again and compared to >> the expected decoded value. This doesn't work so well for floats >> because they cannot be accurately represented in decimal but we try >> our best. > > Good sprintf/scanf/strtod implementations do guarantee that what's > printed and then parsed gets back the same floating point value, as > long as you have printed sufficient decimal digits. > > I'm not sure if FLT_DIG/DLB_DIG are the right number of digits, > though. Glibc's documentation of those is confusing and they might > mean something a little different. > > -- Jamie Yes, conversion fp <-> decimal with sufficient precision can be made exact. However, it's non-trivial[1], and thus neither IEEE-754 nor ISO C require it. IIRC, GNU libc makes it exact. No, FLT_DIG/DBL_DIG is not sufficient precision. You need 7 decimal digits for IEEE single, 17 for IEEE double[2]. FLT_DIG is 6 and DBL_DIG is 15 on my system. An alternative to all that ingenuity is C99's hexadecimal format. [1] Steele & White: How to print floating-point numbers accurately http://portal.acm.org/citation.cfm?id=93559 Clinger: How to Read Floating Point Numbers Accurately, http://portal.acm.org/citation.cfm?id=93557 [2] http://docs.sun.com/source/806-3568/ncg_goldberg.html#1251