From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Timothy Sample Newsgroups: gmane.lisp.guile.bugs Subject: bug#51276: Problems with format and scaling floats Date: Mon, 18 Oct 2021 17:22:49 -0400 Message-ID: <87tuhejady.fsf@ngyro.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="2896"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) To: 51276@debbugs.gnu.org Original-X-From: bug-guile-bounces+guile-bugs=m.gmane-mx.org@gnu.org Mon Oct 18 23:24:13 2021 Return-path: Envelope-to: guile-bugs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mca7A-0000Ss-No for guile-bugs@m.gmane-mx.org; Mon, 18 Oct 2021 23:24:13 +0200 Original-Received: from localhost ([::1]:37754 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mca78-0002uO-RQ for guile-bugs@m.gmane-mx.org; Mon, 18 Oct 2021 17:24:10 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:47076) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mca71-0002sL-Az for bug-guile@gnu.org; Mon, 18 Oct 2021 17:24:05 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:37217) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mca70-0008C9-Ja for bug-guile@gnu.org; Mon, 18 Oct 2021 17:24:03 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1mca70-0002dn-Fc for bug-guile@gnu.org; Mon, 18 Oct 2021 17:24:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Timothy Sample Original-Sender: "Debbugs-submit" Resent-CC: bug-guile@gnu.org Resent-Date: Mon, 18 Oct 2021 21:24:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 51276 X-GNU-PR-Package: guile X-Debbugs-Original-To: bug-guile@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.163459218310081 (code B ref -1); Mon, 18 Oct 2021 21:24:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 18 Oct 2021 21:23:03 +0000 Original-Received: from localhost ([127.0.0.1]:48763 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mca63-0002cW-CL for submit@debbugs.gnu.org; Mon, 18 Oct 2021 17:23:03 -0400 Original-Received: from lists.gnu.org ([209.51.188.17]:39456) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mca60-0002bw-EQ for submit@debbugs.gnu.org; Mon, 18 Oct 2021 17:23:01 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:46864) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mca60-0001b4-3J for bug-guile@gnu.org; Mon, 18 Oct 2021 17:23:00 -0400 Original-Received: from wout4-smtp.messagingengine.com ([64.147.123.20]:38207) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mca5u-0007Em-LH for bug-guile@gnu.org; Mon, 18 Oct 2021 17:22:59 -0400 Original-Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.west.internal (Postfix) with ESMTP id 5A1023201C4E for ; Mon, 18 Oct 2021 17:22:51 -0400 (EDT) Original-Received: from mailfrontend2 ([10.202.2.163]) by compute3.internal (MEProxy); Mon, 18 Oct 2021 17:22:51 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-type:date:from:message-id :mime-version:subject:to:x-me-proxy:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm1; bh=KRQBpzgttjWhskqonYd3NIs9j+uSR YP1/R6tQ0voHQI=; b=LrFG4nBsVPk96A7v3u+lj+bzCpmmpxWPXHVTvBRyAnqB5 64JOQKpXPlGZ95Ai6D+BW0dUKbI52fgwjJBc+24kGES7UM4cMQwwEIaLJ7r9RzJH GF3zMoMXV39LSSfhC8PBsa+M+sCQRQbhEXT67UodlRA+lWx5WOTobf1gIF8ReuLu b5Z5qUnxu6cWQe1iOEzH63RKq6RCe/0Y5yobktYv7kR+naD0H+kNeNK/bsKASeyx 0NParmbLteUVUicgCGJiorpERkTi4m3cvcQNUHnULWTgH+GQjJnTQNhGemPkBoLF kPJwGOptabL50qGc7/wWTDWs/Y0xX5dOjs15Oqo4A== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvtddrvddvtddgudehiecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecunecujfgurhephffvufffkfgfgggtsehmtderre dtreejnecuhfhrohhmpefvihhmohhthhihucfurghmphhlvgcuoehsrghmphhlvghtsehn ghihrhhordgtohhmqeenucggtffrrghtthgvrhhnpedvleelvdeiffffudefhfffhfekke fggfeftddugefhudegtdeklefggeffledvtdenucevlhhushhtvghrufhiiigvpedtnecu rfgrrhgrmhepmhgrihhlfhhrohhmpehsrghmphhlvghtsehnghihrhhordgtohhm X-ME-Proxy: Original-Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Mon, 18 Oct 2021 17:22:50 -0400 (EDT) Received-SPF: pass client-ip=64.147.123.20; envelope-from=samplet@ngyro.com; helo=wout4-smtp.messagingengine.com X-Spam_score_int: -23 X-Spam_score: -2.4 X-Spam_bar: -- X-Spam_report: (-2.4 / 5.0 requ) BAYES_00=-1.9, DKIM_INVALID=0.1, DKIM_SIGNED=0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-guile@gnu.org List-Id: "Bug reports for GUILE, GNU's Ubiquitous Extension Language" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-guile-bounces+guile-bugs=m.gmane-mx.org@gnu.org Original-Sender: "bug-guile" Xref: news.gmane.io gmane.lisp.guile.bugs:10194 Archived-At: --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Hi Guilers, It turns out there=E2=80=99s a little blunder in =E2=80=98format=E2=80=99 (= from =E2=80=98ice-9=E2=80=99). Look at what happens when using the SCALE argument to format a fixed-point float (this is Guile from the Git repo at the time of writing): GNU Guile 3.0.7.6-22120 Copyright (C) 1995-2021 Free Software Foundation, Inc. Guile comes with ABSOLUTELY NO WARRANTY; for details type `,show w'. This program is free software, and you are welcome to redistribute it under certain conditions; type `,show c' for details. Enter `,help' for help. scheme@(guile-user)> (format #t "~,,3f~%" 0.00123) 0.23 $3 =3D #t scheme@(guile-user)> (format #t "~,,1f~%" 0.00123) ice-9/boot-9.scm:1685:16: In procedure raise-exception: Value out of range 0 to 400: -1 Entering a new prompt. Type `,bt' for a backtrace or `,q' to continue. The first example gives the wrong result. Scaling 0.00123 by 3 should yield 1.23, not 0.23. For the second example, instead of 0.0123, we get an error! What=E2=80=99s going on here? Well, our =E2=80=98format=E2=80=99 code comes from SLIB and was written in = 1998, so it=E2=80=99s not easy to explain. There=E2=80=99s so much mutation even a C programmer = would blush! ;) The issue happens in the =E2=80=98format:parse-float=E2=80=99 p= rocedure (which is defined inside of =E2=80=98format=E2=80=99). It normalizes the s= tring representation of a number, and applies the scale argument when needed. It does this by keeping a string of digits and the location of the decimal point. Another thing it keeps track of the leading zeros in a variable called =E2=80=98left-zeros=E2=80=99. Here=E2=80=99s the code that= does the final shifting and places the decimal point: (if (> left-zeros 0) (if (<=3D left-zeros shift) ; shift always > 0 here (format:fn-shiftleft shift) ; shift out 0s (begin (format:fn-shiftleft left-zeros) (set! format:fn-dot (- shift left-zeros)))) (set! format:fn-dot (+ format:fn-dot shift))) The issue is that the cases in the inner =E2=80=98if=E2=80=99 form are reve= rsed. That is, if there are MORE leading zeros than we need to shift, we can just shift. Otherwise (if there are FEWER leading zeros), we need to shift out the zeros and then move the decimal point (=E2=80=98format:fn-dot=E2=80= =99). AFAICS, this bug was in the original SLIB implementation (1998) and has not been fixed since then. It=E2=80=99s been in Guile since 1999. Anyway, that=E2=80=99s more than anyone cares to know.... Here=E2=80=99s a= patch with tests! :) --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0001-ice-9-format-Fix-scaling-floats-with-leading-zeros.patch >From c31d1f5d44343da1201ea1be86bc6b2ac8af6c8d Mon Sep 17 00:00:00 2001 From: Timothy Sample Date: Mon, 18 Oct 2021 17:07:41 -0400 Subject: [PATCH] ice-9 format: Fix scaling floats with leading zeros --- module/ice-9/format.scm | 4 ++-- test-suite/tests/format.test | 10 ++++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/module/ice-9/format.scm b/module/ice-9/format.scm index 48d9c0c84..ee7cba910 100644 --- a/module/ice-9/format.scm +++ b/module/ice-9/format.scm @@ -1359,10 +1359,10 @@ (else (if (> left-zeros 0) (if (<= left-zeros shift) ; shift always > 0 here - (format:fn-shiftleft shift) ; shift out 0s (begin (format:fn-shiftleft left-zeros) - (set! format:fn-dot (- shift left-zeros)))) + (set! format:fn-dot (- shift left-zeros))) + (format:fn-shiftleft shift)) ; shift out 0s (set! format:fn-dot (+ format:fn-dot shift)))))))) (let ((negexp ; expon format m.nnnEee diff --git a/test-suite/tests/format.test b/test-suite/tests/format.test index b9aa7a854..d5111f1c6 100644 --- a/test-suite/tests/format.test +++ b/test-suite/tests/format.test @@ -2,7 +2,7 @@ ;;;; Matthias Koeppe --- June 2001 ;;;; ;;;; Copyright (C) 2001, 2003, 2004, 2006, 2010, 2011, 2012, -;;;; 2014, 2017 Free Software Foundation, Inc. +;;;; 2014, 2017, 2021 Free Software Foundation, Inc. ;;;; ;;;; This library is free software; you can redistribute it and/or ;;;; modify it under the terms of the GNU Lesser General Public @@ -121,7 +121,13 @@ ;; in guile prior to 1.6.9 and 1.8.1, leading zeros were incorrectly ;; stripped, moving the decimal point and giving "25.0" here (pass-if "string 02.5" - (string=? "2.5" (format #f "~f" "02.5")))) + (string=? "2.5" (format #f "~f" "02.5"))) + + (pass-if "scale with few leading zeros" + (string=? "1.23" (format #f "~,,3f" "0.00123"))) + + (pass-if "scale with many leading zeros" + (string=? "0.0123" (format #f "~,,1f" "0.00123")))) ;;; ;;; ~h -- 2.33.0 --=-=-=--