This is the mail archive of the guile@cygnus.com mailing list for the guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: rationals in guile?


On 12 Nov 1998, Valentin Kamyshenko wrote:

> It seems to be, the only difficulty is to insert rationals
> into the tower of types
>                               ---- complex
> 	integers -- rational /
>                              \
>                               ---- floats

If I understand R5RS correctly, the tower has to be

  integers -- rational -- real -- complex

with the additional complexity of distinguishing between exact and inexact
numbers. In guile, integers are represented as immediates or bignums. To
express the number (expt 2 100) => 1267650600228229401496703205376 very
probably a bignum representation will be used (except on 128-bit
architectures :-). However this value could be stored more efficiently
with a floating point representation without loosing exactness. This
means, a number represented as a floating point number may still be an
exact number. 

Although it is assumed that converting an exact integer to an inexact with
exact->inexact will use a floating point representation, this is not
necessarily the case: Bignums could simply have an added inexact-flag
which is set accordingly by exact->inexact and inexact->exact.

I mentioned this to show how much freedom implementations have according
to R5RS (at least if I understand it correctly). For a practical
implementation like guile, implications like 'floating point --> inexact'
are useful.

Still, there are a couple of possibilities to represent the different
numbers and the types they correspond to: (N: integer, Q: rational, R:
real, C: complex)

immediate (exact) --> NQRC
bignum    (exact) --> NQRC
immediate1/immediate2 (exact) --> N if immediate2 == 1, QRC
bignum/immediate2 (exact)     --> N if immediate2 == 1, QRC
immediage1/bignum2 (exact)    --> N if bignum2 == 1,    QRC
bignum1/bignum2 (exact)       --> N if bignum2 == 1,    QRC
float (inexact)               --> N if (= x (round x)), QRC

and, in addition, every combination of the above for the real and
imaginary parts of complex numbers, i. e. 7*7 = 49 possibilities to
represent complex numbers. In total, number? can be true for 56 differing
representations!

Every reduction (like only allowing immediate/immediate or bignum/bignum
for exact rationals, but no combination of immediate and bignum)
simplifies the handling of numbers in guile at the (potential) cost of
efficiency loss.

> I think, it would be better, if this will be done by someone, who
> knows the internals of guile (is it true, that all changes will be
> localized in numbers.c and numbers.h ?). That's why, if
> nobody of gurus has time/possibility to do this now, I thought, it's
> worth to make some preliminary steps, if they are necessary, to make
> this work as easy as possible in future.

Some time ago I took a look at numbers.c and numbers.h to figure out how
to minimize the interface, maybe to make it easier to try out the gmp
representation (GNU multiprecision library). gmp offers very effective
implementations for bignums and rationals with a nice and well designed
interface. The results have made their way onto Jim's TODO list.

Best regards, 
Dirk Herrmann