[racket] Format Bug

From: Carl Eastlund (cce at racket-lang.org)
Date: Tue Jun 8 15:09:02 EDT 2010

On Tue, Jun 8, 2010 at 2:47 PM, Curtis Dutton <curtdutt at gmail.com> wrote:
> Just found a quick little bug with format where it behaves differently than
> printf.
> It appears to treat values formatted through "~v" as if "~a" was specified.
> I've only tested this for exact fractional numbers like 11 5/16 so I'm not
> sure if this happens with other such values.
> For instance:
> (printf "~a~n" (+ 11 (/ 5 16)))
>> 181/16
> (printf "~v~n" (+ 11 (/ 5 16)))
>> 11 5/16
> now format does not behave exactly this way...
> (format "~a~n" (+ 11 (/ 5 16)))
>> "181/16\n"
> (format "~v~n" (+ 11 (/ 5 16)))
>> "181/16\n"
> Is this expected behavior?
> Thanks,
>     Curtis

This is not a bug in format, but rather a peculiarity of the "print"
function and different kinds of output ports.  Try the following in
DrRacket:

(print (+ 11 (/ 5 16)))

(newline)

(with-output-to-string
  (lambda ()
    (print (+ 11 (/ 5 16)))))

The fraction displayed as 5 over 16 with a line between is not, in
fact, a string, but is instead an image.  DrRacket uses a special
output port that can display graphical images; but format and
with-output-to-string use a port that can only accept characters.  The
print function distinguishes between these kinds of ports and decides
how to render fractions accordingly.

--Carl


Posted on the users mailing list.