1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-06-30 06:50:31 +02:00

fix `format' with %f and exact rationals

* module/ice-9/format.scm (format:parse-float): Accept a number, and
  when stringifying the number, first convert to inexact.

* test-suite/tests/format.test ("~f fixed-point"): Add exact rational
  test.
This commit is contained in:
Andy Wingo 2010-06-06 13:46:53 +02:00
parent f42d8bd8ff
commit 625b43acc7
2 changed files with 121 additions and 121 deletions

View file

@ -1206,8 +1206,7 @@
(format:out-inf-nan number width digits #f overch padch))
(digits
(format:parse-float
(if (string? number) number (number->string number)) #t scale)
(format:parse-float number #t scale)
(if (<= (- format:fn-len format:fn-dot) digits)
(format:fn-zfill #f (- digits (- format:fn-len format:fn-dot)))
(format:fn-round digits))
@ -1225,8 +1224,7 @@
(format:fn-out modifier #t)))
(else
(format:parse-float
(if (string? number) number (number->string number)) #t scale)
(format:parse-float number #t scale)
(format:fn-strip)
(if width
(let ((numlen (+ format:fn-len 1)))
@ -1276,8 +1274,7 @@
(+ (- digits scale) 1)
0)
digits)))
(format:parse-float
(if (string? number) number (number->string number)) #f scale)
(format:parse-float number #f scale)
(if (<= (- format:fn-len format:fn-dot) digits)
(format:fn-zfill #f (- digits (- format:fn-len format:fn-dot)))
(format:fn-round digits))
@ -1307,8 +1304,7 @@
(format:en-out edigits expch)))))
(else
(format:parse-float
(if (string? number) number (number->string number)) #f scale)
(format:parse-float number #f scale)
(format:fn-strip)
(if width
(if (and edigits overch (> format:en-len edigits))
@ -1364,8 +1360,7 @@
;; FIXME: this isn't right.
(format:out-inf-nan number width digits edigits overch padch))
(else
(format:parse-float
(if (string? number) number (number->string number)) #t 0)
(format:parse-float number #t 0)
(format:fn-strip)
(let* ((ee (if edigits (+ edigits 2) 4)) ; for the following algorithm
(ww (if width (- width ee) #f)) ; see Steele's CL book p.395
@ -1400,8 +1395,7 @@
(format:out-inf-nan number width digits #f #f padch))
(else
(format:parse-float
(if (string? number) number (number->string number)) #t 0)
(format:parse-float number #t 0)
(if (<= (- format:fn-len format:fn-dot) digits)
(format:fn-zfill #f (- digits (- format:fn-len format:fn-dot)))
(format:fn-round digits))
@ -1452,7 +1446,10 @@
(format:en-pos? #t) ; exponent positive?
(format:parse-float
(lambda (num-str fixed? scale)
(lambda (num fixed? scale)
(let ((num-str (if (string? num)
num
(number->string (exact->inexact num)))))
(set! format:fn-pos? #t)
(set! format:fn-len 0)
(set! format:fn-dot #f)
@ -1565,7 +1562,7 @@
((char=? c #\d) #t) ; decimal radix prefix
((char=? c #\#) #t)
(else
(format:error "illegal character `~c' in number->string" c))))))
(format:error "illegal character `~c' in number->string" c)))))))
(format:en-int
(lambda () ; convert exponent string to integer

View file

@ -1,7 +1,7 @@
;;;; format.test --- test suite for Guile's CL-ish format -*- scheme -*-
;;;; Matthias Koeppe <mkoeppe@mail.math.uni-magdeburg.de> --- June 2001
;;;;
;;;; Copyright (C) 2001, 2003, 2004, 2006 Free Software Foundation, Inc.
;;;; Copyright (C) 2001, 2003, 2004, 2006, 2010 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
@ -82,6 +82,9 @@
(pass-if "1.5"
(string=? "1.5" (format #f "~f" 1.5)))
(pass-if "3/2"
(string=? "1.5" (format #f "~f" 3/2)))
;; 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"