[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: printing floats/integer



"Sean Heukels" <sean77=cuthere=@dds.nl> writes:
> I wrote a small module for integers. The variable is formatted and returned.
> 
> "1         " returns as "1"
> 
> Now this doesn't work with floats. for example if I want to print "1.22221"
> I
> dont want to see it as "1.222221000000000000000" or
> "1.222222                  "
> 
> Does anyone know how I can solve this ??

The first thing to understand is that there are many real numbers
which cannot be represented in floating point number system of
computers.  That's just the facts.

Second, if you simply don't want spaces around your numbers, why not
try STRTRIM?

One of the most sophisticated answers might be found in the following
paper by Burger and Dybvig, "Printing Floating-Point Numbers Quickly
and Accurately:"

http://citeseer.nj.nec.com/28233.html

Unfortunately that's probably overkill, and too hard to implement in
IDL.

You've seen Bob S's implementation.  I have a program called INPUTFORM
on my web page which prints a number as a string, such that it can be
read again by the IDL parser.  Like this:

IDL> print, inputform(1.22221)
1.22221E

You see the "E" indicates that it is a single-precision floating point
number, to distinguish it unambiguously from double precision or
integers.  Just as Bob's code is not too pretty (sorry Bob), neither
is the code in INPUTFORM.  Basically it tries printing the number with
both the "G" and "D" output formats and takes whichever is shorter.

Craig

INPUTFORM can be found at
http://cow.physics.wisc.edu/~craigm/idl/idl.html

-- 
--------------------------------------------------------------------------
Craig B. Markwardt, Ph.D.         EMAIL:    craigmnet@cow.physics.wisc.edu
Astrophysics, IDL, Finance, Derivatives | Remove "net" for better response
--------------------------------------------------------------------------