From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: lloda Newsgroups: gmane.lisp.guile.bugs Subject: bug#51276: Problems with format and scaling floats Date: Tue, 19 Oct 2021 03:02:22 +0200 Message-ID: References: <87tuhejady.fsf@ngyro.com> Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.120.23.2.7\)) Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="26838"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 51276@debbugs.gnu.org To: Timothy Sample Original-X-From: bug-guile-bounces+guile-bugs=m.gmane-mx.org@gnu.org Tue Oct 19 03:03:21 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 1mcdXF-0006gJ-Eu for guile-bugs@m.gmane-mx.org; Tue, 19 Oct 2021 03:03:21 +0200 Original-Received: from localhost ([::1]:36306 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mcdX9-0004Y4-05 for guile-bugs@m.gmane-mx.org; Mon, 18 Oct 2021 21:03:15 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:51948) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mcdWw-0004Xw-7a for bug-guile@gnu.org; Mon, 18 Oct 2021 21:03:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:37357) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mcdWv-0002Pb-V5 for bug-guile@gnu.org; Mon, 18 Oct 2021 21:03:01 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1mcdWv-0001bz-NJ for bug-guile@gnu.org; Mon, 18 Oct 2021 21:03:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: lloda Original-Sender: "Debbugs-submit" Resent-CC: bug-guile@gnu.org Resent-Date: Tue, 19 Oct 2021 01:03:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 51276 X-GNU-PR-Package: guile Original-Received: via spool by 51276-submit@debbugs.gnu.org id=B51276.16346053565942 (code B ref 51276); Tue, 19 Oct 2021 01:03:01 +0000 Original-Received: (at 51276) by debbugs.gnu.org; 19 Oct 2021 01:02:36 +0000 Original-Received: from localhost ([127.0.0.1]:48903 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mcdWV-0001XX-Tz for submit@debbugs.gnu.org; Mon, 18 Oct 2021 21:02:36 -0400 Original-Received: from mta-14-3.privateemail.com ([198.54.127.110]:53563) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mcdWR-0001R8-Pe for 51276@debbugs.gnu.org; Mon, 18 Oct 2021 21:02:34 -0400 Original-Received: from mta-14.privateemail.com (localhost [127.0.0.1]) by mta-14.privateemail.com (Postfix) with ESMTP id C70C718000A3; Mon, 18 Oct 2021 21:02:25 -0400 (EDT) Original-Received: from [192.168.1.105] (unknown [10.20.151.206]) by mta-14.privateemail.com (Postfix) with ESMTPA id 08F78180009E; Mon, 18 Oct 2021 21:02:24 -0400 (EDT) In-Reply-To: <87tuhejady.fsf@ngyro.com> X-Mailer: Apple Mail (2.3608.120.23.2.7) X-Virus-Scanned: ClamAV using ClamSMTP 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:10195 Archived-At: Thank you very much, patch applied! Now that you've dived into (ice-9 format), what about this one: (format #t "~,,2f~%" 0.01) =3D> 1.0 (format #t "~,,3f~%" 0.01) ; !! =3D> 010.0 (format #t "~,,3f~%" 0.001) =3D> 1.0 (format #t "~,,4f~%" 0.001) ; !! =3D> 0010.0 (format #t "~,,4f~%" 0.0001) =3D> 1.0 (format #t "~,,5f~%" 0.0001) ; ok somehow... =3D> 10.0 regards > On 18 Oct 2021, at 23:22, Timothy Sample wrote: >=20 > Hi Guilers, >=20 > 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): >=20 > GNU Guile 3.0.7.6-22120 > Copyright (C) 1995-2021 Free Software Foundation, Inc. >=20 > 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. >=20 > 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 >=20 > Entering a new prompt. Type `,bt' for a backtrace or `,q' to = continue. >=20 > 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? >=20 > 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= procedure > (which is defined inside of =E2=80=98format=E2=80=99). It normalizes = the string > 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: >=20 > (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))) >=20 > The issue is that the cases in the inner =E2=80=98if=E2=80=99 form are = reversed. 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). >=20 > 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. >=20 > Anyway, that=E2=80=99s more than anyone cares to know.... Here=E2=80=99= s a patch with > tests! :) >=20 > =46rom 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 >=20 > --- > module/ice-9/format.scm | 4 ++-- > test-suite/tests/format.test | 10 ++++++++-- > 2 files changed, 10 insertions(+), 4 deletions(-) >=20 > 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 (<=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 (- shift = left-zeros))) > + (format:fn-shiftleft shift)) ; shift = out 0s > (set! format:fn-dot (+ format:fn-dot = shift)))))))) >=20 > (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=3D? "2.5" (format #f "~f" "02.5")))) > + (string=3D? "2.5" (format #f "~f" "02.5"))) > + > + (pass-if "scale with few leading zeros" > + (string=3D? "1.23" (format #f "~,,3f" "0.00123"))) > + > + (pass-if "scale with many leading zeros" > + (string=3D? "0.0123" (format #f "~,,1f" "0.00123")))) >=20 > ;;; > ;;; ~h > --=20 > 2.33.0 >=20