Frank Christoph writes:

    It seems to me there is a tension between using show as a way of doing quick
    and dirty pretty-printing, and as a way of getting a portable representation of 
data.

This is a "bug" in hugs.

To illustrate the problem, the next floating point number after 5.0 is 5.00000047,
which hugs also prints as 5.0. One might argue that to display it as 5.0000005 would
be misleading, since this number is the closest representable to everything from 
5.0000003 to 5.0000007. On the other hand, to print only the "certain" digits makes
show many-to-one, with unfortunate consequences when show and read are
used as a portable way to store data.

In fact, hugs' behaviour isn't consistent with the Haskell 98 report.

The Numeric library refers to Burger and Dybvig, PLDI 96 for its floating point output
routines. But Burger and Dybvig say "Our algorithm generates the shortest, correctly
rounded output string from which the original floating point number can be recovered
when input" -- that is, read . show should be the identity. Sure enough, the Numeric
library exports a function showFloat which implements Burger and Dybvig's
algorithm, and showFloat 5.00000047 == "5.0000005". According to the Haskell
report, show for floating point numbers should call showFloat -- but under hugs, it
doesn't, and hence the strange behaviour.

This behaviour is actually documented in the hugs manual, under "conformance with
Haskell 98". So as a documented bug, it must be a feature -- but perhaps one which
might be removed in a future version?

John Hughes


Reply via email to