[cl-json-devel] Encoding DOUBLE-FLOAT

Boris Smilga boris.smilga at gmail.com
Thu Jun 23 12:05:23 UTC 2011


On Thu, Jun 23, 2011 at 1:04 AM, Robert Goldman <rpgoldman at sift.info> wrote:
> Would it be better to rewrite this from shortest to longest as:
>
> (defun write-json-number (nr stream)
>  "Write the JSON representation of the number NR to STREAM."
>  (typecase nr
>    (integer (format stream "~d" nr))
>    (real (let ((*read-default-float-format*
>                 (etypecase nr
>                   (short-float 'short-float)
>                   (single-float 'single-float)
>                   (double-float 'double-float)
>                   (long-float 'long-float))))
>            (format stream "~f" nr)))
>    (t (unencodable-value-error nr 'write-json-number))))

What about rationals?  They too are a subtype of REAL, but your
etypecase doesn't take them into account.

> This still yields warnings on Clozure, because SHORT-FLOAT ==
> SINGLE-FLOAT and DOUBLE-FLOAT == LONG-FLOAT (IIUC), but I think that's
> implementation-dependent, so we can't do away with the intermediates in
> general, can we?
>
> Not sure how to muffle these warnings, or even if it's desirable, since
> the Clozure implementers might split apart single and short and double
> and long at some point, right?

In principle, yes (although in practive such design choices tend to be
very stable, undergoing changes only when radically different hardware
architectures become available).  If these warnings bother you so
much, you might just use #-CCL et al. to exclude the TYPECASE clauses
that cause them.

Yours,
 - B. Sm.




More information about the cl-json-devel mailing list