From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Bengt Richter Newsgroups: gmane.lisp.guile.bugs Subject: bug#51276: Problems with format and scaling floats Date: Tue, 19 Oct 2021 10:42:49 +0200 Message-ID: <20211019084249.GA2281@LionPure> References: <87tuhejady.fsf@ngyro.com> Reply-To: Bengt Richter Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="26239"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Mutt/1.10.1 (2018-07-13) 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 10:44:12 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 1mckjD-0006cp-Qx for guile-bugs@m.gmane-mx.org; Tue, 19 Oct 2021 10:44:11 +0200 Original-Received: from localhost ([::1]:40882 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mckjB-00010m-NR for guile-bugs@m.gmane-mx.org; Tue, 19 Oct 2021 04:44:09 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:46254) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mckj4-00010c-Ts for bug-guile@gnu.org; Tue, 19 Oct 2021 04:44:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:37823) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mckj4-0002uB-L9 for bug-guile@gnu.org; Tue, 19 Oct 2021 04:44:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1mckj4-0002qA-DJ for bug-guile@gnu.org; Tue, 19 Oct 2021 04:44:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Bengt Richter Original-Sender: "Debbugs-submit" Resent-CC: bug-guile@gnu.org Resent-Date: Tue, 19 Oct 2021 08:44:02 +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.163463298710844 (code B ref 51276); Tue, 19 Oct 2021 08:44:02 +0000 Original-Received: (at 51276) by debbugs.gnu.org; 19 Oct 2021 08:43:07 +0000 Original-Received: from localhost ([127.0.0.1]:49369 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mckiB-0002oq-GP for submit@debbugs.gnu.org; Tue, 19 Oct 2021 04:43:07 -0400 Original-Received: from imta-35.everyone.net ([216.200.145.35]:60104 helo=imta-38.everyone.net) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mcki7-0002oX-6a for 51276@debbugs.gnu.org; Tue, 19 Oct 2021 04:43:06 -0400 Original-Received: from pps.filterd (m0004961.ppops.net [127.0.0.1]) by imta-38.everyone.net (8.16.0.43/8.16.0.43) with SMTP id 19J8gHFi021769; Tue, 19 Oct 2021 01:43:01 -0700 X-Eon-Originating-Account: vcvb2okAtsKTSb1vohNFSKl1CkBaOAnUGYKnjtkqgKA X-Eon-Dm: m0116953.ppops.net Original-Received: by m0116953.mta.everyone.net (EON-AUTHRELAY2 - 53b92b76) id m0116953.615b1d6e.1e9871; Tue, 19 Oct 2021 01:43:00 -0700 X-Eon-Sig: AQMHrIJhboUU7HAV4gIAAAAC,7deaa4179e74c43289fc9f3733a515cb X-Eip: di0jzi7bKTOwjG-D0BYV-E2onsHCNT29_ojitNsaH6Y Content-Disposition: inline In-Reply-To: <87tuhejady.fsf@ngyro.com> X-Proofpoint-ORIG-GUID: M5vCLD4X9qfOUqC6S-TQU-z15W3yvqQI X-Proofpoint-GUID: M5vCLD4X9qfOUqC6S-TQU-z15W3yvqQI X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.425, 18.0.790 definitions=2021-10-18_07:2021-10-18, 2021-10-18 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 impostorscore=0 lowpriorityscore=0 priorityscore=1501 phishscore=0 adultscore=0 mlxlogscore=999 clxscore=1034 malwarescore=0 spamscore=0 bulkscore=0 suspectscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2109230001 definitions=main-2110190053 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:10196 Archived-At: Nice catch :) On +2021-10-18 17:22:49 -0400, Timothy Sample wrote: > Hi Guilers, > > It turns out there’s a little blunder in ‘format’ (from ‘ice-9’). 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 = #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’s going on here? > > Well, our ‘format’ code comes from SLIB and was written in 1998, so it’s > not easy to explain. There’s so much mutation even a C programmer would > blush! ;) The issue happens in the ‘format:parse-float’ procedure > (which is defined inside of ‘format’). 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 ‘left-zeros’. Here’s the code that does the final > shifting and places the decimal point: > > (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 (+ format:fn-dot shift))) > > The issue is that the cases in the inner ‘if’ 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 (‘format:fn-dot’). > > AFAICS, this bug was in the original SLIB implementation (1998) and has > not been fixed since then. It’s been in Guile since 1999. > > Anyway, that’s more than anyone cares to know.... Here’s a patch with > tests! :) > 1999 until now (2021), /and/ reliably reproduced all that time! :) -- Regards, Bengt Richter