This is the mail archive of the guile@sourceware.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]

two modules: (ice-9 rationals), (ice-9 rational-object)


[Note: I am not yet on this mailing list, do to email problems, so
it'd be nice if replies to this message would be CC'd back to me,
with TO JULIAN: on the subject line.  Sorry for in inconvenience.]


Greetings.

Attached is a gzipped tarball containing two scheme files named
rationals.scm and rational-object.scm, which are respectively defined
as (ice-9 rationals) and (ice-9 rational-object).


(ice-9 rationals)

This module implements the r5rs functions `numerator' and
`denominator' which act as specified in the r5rs except that they work
on inexact numbers, which are what Guile evaluates rational literals
to.  It also has several other useful and related functions, which are
as follows:
   rational->pair
   pair->rational
   rational->real    ; reciprocal: (simplify-rational REAL)
   rational->integer ; reciprocal: (simplify-rational INTEGER)
   rational->string
     ; reciprocal: (simplify-rational (string->number STR))

Simplify-rational has a very long docstring complete with examples,
describing its functionality.


(ice-9 rational-object)

This module has functionality almost exactly equivalent to that of
(ice-9 rationals) (and indeed, there is much code-duplication between
them), except that its functions mostly act on or yield objects of the
class <rational>, whereas the functions of (ice-9 rationals) mostly
act on or yield pairs.

Through goops, the rational objects defined by (ice-9 rational-object)
can act like actual number numbers.  There are some limitations, which
are listed here:
        1) Since goops wont make methods for some primitives like
           `expt', these functions do not act intelligently on
           instances of <rational>.  You can hack this by converting
           the rational to a real for the operation, and future
           versions of (ice-9 rational-object) will either do this in
           a way offered by goops or rename all of the bad primitives
           and work around them with methods like:
             (define-method expt ((rat <rational>) (n <number>))
               (make-rational
                 (%expt (numerator rat) n)
                 (%expt (denominator rat) n)))
             (define-method expt (n1 n2)
               (%expt n1 n2))
         2) You must *initialize* rational objects, using
            make-rational.  You don't have to do that for normal
            numbers.

You should NOT create instances of <rational> with `make', unless you
are prepared to simplify the rational number yourself.  In the current
implementation the numerator and the denominator slots of <rational>
are fixed at initialization and do not have setters; make-rational
does all the simplifying with a wide variety of arguments to create
the instance -- see its docstring.

Make-rational is almost identical to simplify-rational of (ice-9
rationals), except that it returns instances of <rational>.  Methods
for all the other functions of (ice-9 rationals) exist in (ice-9
rational-object) but the two may not coexist (yet -- I may have to
work on how Goops does its generic functions or see if its MOP will
suffice).


Note that the functions in both modules tend to become sluggish for
very high numerators or denominators.  The only real solution I see to
this is to implement rationals as primitive types in Guile.



Thank you,
  Julian Fondren

ps: It'd be just wonderful if someone could contribute a definition
for the r5rs function `rationalize' -- I simultaneously fail to
understand its purpose and its implementation.

  
Here are two long usage examples for (ice-9 rationals) and (ice-9
rational-object).

guile> (use-modules (ice-9 rationals))
guile> (rational->string 3/9)
"1/3"
guile> (rational->string .25)
"1/4"
guile> (simplify-rational 33/99)
(1 . 3)
guile> (pair->rational '(1 . 3))
0.333333333333333
guile> (simplify-rational (string->number "12/18"))
(2 . 3)
guile> (numerator 3.14)
157
guile> (denominator 3.14)
50
guile> (rational->integer 3.14)
3
guile> (rational->integer 3/9)
0
guile> (rational->integer 22/11)
2
guile> (rational->real '(2 . 4))
0.5


guile> (use-modules (ice-9 rational-object))
guile> (define rat (make-rational 3/9))
guile> rat
<rational: 1/3>
guile> (+ rat rat)
<rational: 2/3>
guile> (+ rat rat rat)
<rational: 1/1>
guile> (make-rational 33/99)
<rational: 1/3>
guile> (make-rational .25)
<rational: 1/4>
guile> (define rat (make-rational '(1 . 3)))
guile> rat
<rational: 1/3>
guile> (* rat 3)
<rational: 1/1>
guile> (- rat 1/3)
<rational: 0/1>
guile> (* rat 0)
<rational: 0/1>
guile> (numerator rat)
1
guile> (denominator rat)
3
guile> (rational->pair rat)
(1 . 3)
guile> (rational->real rat)
0.333333333333333
guile> (rational->integer rat)
0
guile> (rational->string rat)
"1/3"
guile> (/ rat 3)
<rational: 1/9>
guile> (+ rat 2.3)
<rational: 79/30>
guile> (rational->real (+ rat 2.3))
2.63333333333333

Rat.tgz


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