From: rinosh sasidharan
Subject: lisp langauage question.
Date: 
Message-ID: <01bd9f8b$1e7b27a0$3615eec3@default>
i need some help with lisp. how do you display decimals. i.e (/ 9 4) = 9/4.
how do you display decimals.

From: Frank Adrian
Subject: Re: lisp langauage question.
Date: 
Message-ID: <gYak1.93$Rk2.116921@client.news.psi.net>
rinosh sasidharan wrote in message <··························@default>...
>i need some help with lisp. how do you display decimals. i.e (/ 9 4) = 9/4.
>how do you display decimals.

Convert the number to floating point by using the coerce or float functions.
If you want more control over number of digits displayed, use format.
--
Frank A. Adrian
First DataBank

············@firstdatabank.com (W)
······@europa.com (H)

This message does not necessarily reflect those of my employer,
its parent company, or any of the co-subsidiaries of the parent
company.
From: Kent M Pitman
Subject: Re: lisp langauage question.
Date: 
Message-ID: <sfwvhpq3d43.fsf@world.std.com>
"rinosh sasidharan" <······@eudoramail.com> writes:

> i need some help with lisp. how do you display decimals. i.e (/ 9 4) = 9/4.
> how do you display decimals.

You're not asking the right question.  The number 2.25 is a FLOAT,
which is a specific representation used by computers as a placeholder
for real numbers but which is inherently unable to represent all real
numbers because of range and precision problems.  Consequently, every
float is at least potentially an inexact representation.  For example,
0.3333 is not 1/3 and no amounto of adding 3's will fix it even though
some amount of adding 3's will exceed the precision of the machine's
FLOAT representation.

Lisp has a datatype called a RATIONAL which is represented exactly as
the ratio of two integers.  When you divide 9 by 4, you get 9/4.  This
is the ratio in reduced form.  For example, (/ 10 4) will give you
5/2.  And, in particular, (/ 2 6) will give you 1/3 as the answer and
this answer is exact in that it is a rational number which is completely
accurately represented as the ratio of two integers.  

I think the operation you want is FLOAT, which tries to find an 
approximation in the other datatype.  For example, 
(float 1/3) => 0.33333334 
in my implementation.  But note the 4.  This is an artifact of how
floats are stored.  You get round-off error to the precision of the
machine, and that error will accumulate if you do lots of float operations.
In Lisp, you should try to stick with rationals for as long as you can
in order not to accumulate round-off error, and convert to float only 
if the rational computations are the source of too much program slowness
or if you've finished operating on the number and just want to see
it in APPROXIMATE decimal form.

There are also operations that will take numbers like 0.33333334 and
try to turn them back into a rational.  This is a dangerous operation
to do on an inexact number, turning it back into an exact number,
because you can trick yourself into believing it's a number that it's
not.  Strictly, you might expect this to become  333333334/100000000,
though that would reduce immediately to 1666667/50000000.  But in fact,
since the number is stored in binary and not decimal, and the last digit
is only half-there (which is why it looks to have rounded up where you
expect it to have rounded down on the last digit), 
(rational 0.33333334) => 11184811/33554432
A function RATIONALIZE exists that works harder (read: more slowly) to find a more 
satisfactory result.
(rationalize 0.33333334) => 1/3
Sometimes it doesn't get you exactly what you want, but often it's 
prettier than what RATIONAL would do and for some applications prettier
may also be right. 

If, on the other hand, all you wanted to do was take 9/4 and
display it without changing its datatype, you can use FORMAT's
~$, ~E, ~F, and ~G operations. Each of these has ways to control
how many digits get printed, etc.  (~$ defaults to 2 digits because
it's usually used with money; the rest are traditional floating
point display routines that were inspired by the E, F, and G operations
in FORTRAN's FORMAT operator.)  But in the simple case with no
preference specified, you'll see:

(dolist (x (list (/ 9 4) (/ 1 3) (/ 4 2)))
  (terpri)
  (format t "~$ ~E ~F ~G" x x x x))
2.25 2.25E+0 2.25 2.25   
0.33 3.3333334E-1 0.33333334 0.33333334
2.00 2.0E+0 2.0 2.