[mercury-users] Printing reasonable floats?

Julian Fondren ayrnieu at gmail.com
Sun Feb 11 23:19:11 AEDT 2007


Doug Auclair wrote:
> Is there a way to control the runtime representation of (nested)
> floats that write displays?
> When I print my switch, I say:
>   print(Switch)

> Is it possible to set some value in the io state to control how
> float is printed?

Nobody has responded, so: AFAICT, no.  My quick research showed
that:

io.print(Switch) like that gets resolved to:

  io.print(Stream, Term, !IO) :-
    io.set_output_stream(Stream, OrigStream, !IO),
    io.do_print(canonicalize, Term, !IO),
    io.set_output_stream(OrigStream, _Stream, !IO).

and then to:

  :- pragma export(io.print(in, in(canonicalize), in, di, uo),
      "ML_io_print_can_to_stream").

and then to some C that I couldn't find the source of, except in
the generated io.c, which seems itself to call an io.print

of course, you can roll something of your own using io:format or
string:format (code example trails this message).

Hope that helps,
Julian


:- pred dump_flist(list(float)::in, io::di, io::uo) is det.
dump_flist(!.L, !IO) :-
        get_float_precision(P, !IO),
        Fun = (func(F) = S :- string.format("%.*f", [i(P), f(F)], S)),
        !:L = list.map(Fun, !.L),
        io.print(!.L, !IO),
        io.nl(!IO).

:- pragma c_header_code("static int float_precision = 15;").

:- pred get_float_precision(int::out, io::di, io::uo) is det.
:- pragma c_code(get_float_precision(P::out, IO1::di, IO::uo),
        [will_not_call_mercury], "{
                P = float_precision;
                IO = IO1;
        }").

:- pred set_float_precision(int::in, io::di, io::uo) is det.
:- pragma c_code(set_float_precision(P::in, IO1::di, IO::uo),
        [will_not_call_mercury], "{
                float_precision = P;
                IO= IO1;
        }").
--------------------------------------------------------------------------
mercury-users mailing list
Post messages to:       mercury-users at csse.unimelb.edu.au
Administrative Queries: owner-mercury-users at csse.unimelb.edu.au
Subscriptions:          mercury-users-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the users mailing list