[erlang-questions] pre-EEP: accurate float printing (replacement for io_lib_format:fwrite_g/1)

Bob Ippolito bob@REDACTED
Wed Dec 12 00:58:07 CET 2007


On 12/11/07, Robert Virding <rvirding@REDACTED> wrote:
> On 11 Dec 2007 10:10:48 +0100, Bjorn Gustavsson <bjorn@REDACTED>
> wrote:
> > "Bob Ippolito" <bob@REDACTED> writes:
> >
> > > ~g with no precision specification (io_lib_format:fwrite_g/1) is a
> > > perfect target and it hits every use the shell has, ~f and ~e I don't
> > > care about. Having yet another float format would be completely stupid
> > > and confusing to anyone that doesn't know the back story for why
> > > supposed backward compatibility required the need for another format.
> > > Other languages have undergone this type of change with no ill effects
> > > that I've heard of (e.g. TCL did this in 2003 [1]).
> >
> > I agree. If there are no compatibility problems *in practice*, we
> > should simply make the change.
>
> I agree, we should change if no real problems are found. What I don't
> understand is why just have it for  ~g with no precision and not for all
> cases of ~g, ~f and ~e? They are really just shifting the decimal dot and
> fixing the exponent.

The current implementation does what ~g does: if there's going to be a
lot of zeros, then it switches to ~e, otherwise it's ~f. We only
really care about ~g (if it's implemented with this algorithm, with no
explicit precision specification) because our use cases are the shell
and accurate and compact text serialization (JSON specifically).

1> mochinum:digits(0.1).
"0.1"
2> mochinum:digits(0.0000001).
"1.0e-7"
3> mochinum:digits(1.0000005).
"1.0000005"
4> 0.1.
0.100000
5> 0.0000001.
1.00000e-7
6> 1.0000005.
1.00000

-bob



More information about the erlang-questions mailing list