floating point numbers format




On Thu, 11 Jun 2009, Julien Martin wrote:

< Giovanni,
< I'd really like to know the answer to the question you asked. Any luck
< elsewhere?
< Julien.
< 
< 2009/6/10 Giovanni <giovannibis at inwind.it>
< 
< >
< >  Hi all. It's possible to have an output like the one generated by
< > '%.2f' in a printf() function, in the whole maxima document? The most
< > similar options i've found it's fpprintprec, but it's not the same
< > (it does not control the number of decimal numbers to display).
 
I haven't looked at the maxima source to see if maxima has the native
ability, but here is a very simple way to achieve what you want.
Define a 'pretty-print' function for floats, and use fullmap and the
ability to define post-fix operators:

(%i2) pp(x) := if floatnump(x) then parse_string(printf(false,"~,3f",x))
else x$
(%i3) postfix("@@")$
(%i4) "@@"([x]) := part(fullmap(pp,x),1)$
(%i5) float(%e);
(%o5) 2.718281828459045
(%i6) % @@;
(%o6) 2.718
(%i7) solve(x^3-x+1,x)$
(%i8) expand(float(%)) @@;
(%o8) [x = 0.662-0.562*%i,x = 0.562*%i+0.662,x = -1.325]

Leo

-- 
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.