From debbugs-submit-bounces@debbugs.gnu.org Tue Mar 11 09:40:50 2014 Received: (at submit) by debbugs.gnu.org; 11 Mar 2014 13:40:50 +0000 Received: from localhost ([127.0.0.1]:32798 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1WNMvJ-0000GE-UI for submit@debbugs.gnu.org; Tue, 11 Mar 2014 09:40:50 -0400 Received: from eggs.gnu.org ([208.118.235.92]:39851) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1WNMvF-0000G3-Sz for submit@debbugs.gnu.org; Tue, 11 Mar 2014 09:40:46 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WNMvE-0005yn-KT for submit@debbugs.gnu.org; Tue, 11 Mar 2014 09:40:45 -0400 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on eggs.gnu.org X-Spam-Level: X-Spam-Status: No, score=0.8 required=5.0 tests=BAYES_50,T_RP_MATCHES_RCVD autolearn=disabled version=3.3.2 Received: from lists.gnu.org ([2001:4830:134:3::11]:46882) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WNMvE-0005yj-He for submit@debbugs.gnu.org; Tue, 11 Mar 2014 09:40:44 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41569) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WNMvD-0008CA-KI for bug-guile@gnu.org; Tue, 11 Mar 2014 09:40:44 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WNMvB-0005yG-J5 for bug-guile@gnu.org; Tue, 11 Mar 2014 09:40:43 -0400 Received: from fencepost.gnu.org ([2001:4830:134:3::e]:35859) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WNMvB-0005y9-90 for bug-guile@gnu.org; Tue, 11 Mar 2014 09:40:41 -0400 Received: from localhost ([127.0.0.1]:43032 helo=lola) by fencepost.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WNMvA-0003s6-TX for bug-guile@gnu.org; Tue, 11 Mar 2014 09:40:41 -0400 Received: by lola (Postfix, from userid 1000) id 70AB9E278D; Tue, 11 Mar 2014 14:40:40 +0100 (CET) From: David Kastrup To: bug-guile@gnu.org Subject: Bad default mantissa width for ~g format string Date: Tue, 11 Mar 2014 14:40:40 +0100 Message-ID: <87lhwg7t0n.fsf@fencepost.gnu.org> MIME-Version: 1.0 Content-Type: text/plain X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2001:4830:134:3::11 X-Spam-Score: -5.0 (-----) X-Debbugs-Envelope-To: submit X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -5.0 (-----) scheme@(guile-user)> (format #f "~g" (/ 1e100 3)) $19 = "3333333333333333000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0 " That, frankly, is pretty ludicrous. The reserved mantissa width should at least be constrained to the actually available precision of the data type. If it isn't, there is no useful way to output a variable with minimal width. Using ~S instead results in the much more reasonable "3.333333333333333e99" but it will not convert fractions to a floating point representation: scheme@(guile-user)> (format #f "~g" (/ 10000000000000000000000000000000 3)) $22 = "3333333333333333600000000000000.0 " scheme@(guile-user)> (format #f "~S" (/ 10000000000000000000000000000000 3)) $23 = "10000000000000000000000000000000/3" scheme@(guile-user)> -- David Kastrup From debbugs-submit-bounces@debbugs.gnu.org Tue Mar 11 11:22:07 2014 Received: (at 16987) by debbugs.gnu.org; 11 Mar 2014 15:22:07 +0000 Received: from localhost ([127.0.0.1]:33473 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1WNOVK-0003Gu-T4 for submit@debbugs.gnu.org; Tue, 11 Mar 2014 11:22:07 -0400 Received: from world.peace.net ([96.39.62.75]:51912) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1WNOV9-0003GH-Md for 16987@debbugs.gnu.org; Tue, 11 Mar 2014 11:22:05 -0400 Received: from 209-6-91-212.c3-0.smr-ubr1.sbo-smr.ma.cable.rcn.com ([209.6.91.212] helo=yeeloong.lan) by world.peace.net with esmtpsa (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.72) (envelope-from ) id 1WNOV2-0008A2-B8; Tue, 11 Mar 2014 11:21:48 -0400 From: Mark H Weaver To: David Kastrup Subject: Re: bug#16987: Bad default mantissa width for ~g format string References: <87lhwg7t0n.fsf@fencepost.gnu.org> Date: Tue, 11 Mar 2014 11:21:36 -0400 In-Reply-To: <87lhwg7t0n.fsf@fencepost.gnu.org> (David Kastrup's message of "Tue, 11 Mar 2014 14:40:40 +0100") Message-ID: <87y50g92wv.fsf@yeeloong.lan> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-Spam-Score: 0.0 (/) X-Debbugs-Envelope-To: 16987 Cc: 16987@debbugs.gnu.org X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: 0.0 (/) David Kastrup writes: > scheme@(guile-user)> (format #f "~g" (/ 1e100 3)) > $19 = "3333333333333333000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0 " > > That, frankly, is pretty ludicrous. The reserved mantissa width should > at least be constrained to the actually available precision of the data > type. Agreed. > If it isn't, there is no useful way to output a variable with > minimal width. Using ~S instead results in the much more reasonable > "3.333333333333333e99" but it will not convert fractions to a floating > point representation: > > scheme@(guile-user)> (format #f "~g" (/ 10000000000000000000000000000000 3)) > $22 = "3333333333333333600000000000000.0 " > scheme@(guile-user)> (format #f "~S" (/ 10000000000000000000000000000000 3)) > $23 = "10000000000000000000000000000000/3" > scheme@(guile-user)> As a workaround, you can force decimal output with ~S by converting the exact fraction to an inexact floating point number before passing it to 'format', using 'exact->inexact': scheme@(guile-user)> (format #f "~S" (exact->inexact (/ 10000000000000000000000000000000 3))) $4 = "3.3333333333333336e30" Note that 'exact->inexact' works even if the number passed to it is already inexact, in which case it's a no-op. At some point we'll make ~g do the right thing for exact fractions, so that the last digit printed is a 3, not 6. For now, it can't be helped because the value is being rounded twice: once in the conversion to inexact binary, and again when converting from binary to decimal. Thanks, Mark