precision loss in ~F format directive when given a fixnum

Bug #1084390 reported by Douglas Katzman
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
SBCL
New
Undecided
Unassigned

Bug Description

* (integer-length 556677893)
30
* (format nil "~F" 556677893)
"556677900.0"

This is because the CLHS is being taken quite literally: "If arg is a rational number, then it is coerced to be a single float and then printed."
However, it goes on: "Alternatively, an implementation is permitted to process a rational number by any other method that has essentially the same behavior but avoids loss of precision or overflow because of the coercion."

So maybe this SINGLE-FLOAT coercion can be changed to DOUBLE-FLOAT for an easy way to do a little better?
Otherwise the caller can do the coercion himself, which is fine. But I was kind of surprised by this limitation.

(defun format-fixed (stream number w d k ovf pad atsign)
  (typecase number
...
    (rational
     (format-fixed-aux stream (coerce number 'single-float) ; << HERE
                       w d k ovf pad atsign))

Tested in not-quite-latest code, but pulled latest from git and it's the same.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.